java - 带有 header 参数的Get方法无法正常工作android

标签 java android utf-8

我有以下 JSON:

{
  "category": [{
    "id": "90",
    "user_id": "1",
    "category_id": "27",
    "name": "આણંદ કોમર્સિયલ લેયર",
    "order": "0",
    "created_at": "2014-05-03 17:09:54",
    "updated_at": "2014-05-03 17:09:54",
    "deleted": "0",
    "subtopics": [{
      "id": "203",
      "user_id": "1",
      "category_id": "27",
      "subcategory_id": "90",
      "name": "આણંદ કોમર્સિયલ લેયર (સંકર જાત)",
      "order": "0",
      "details": "<p style=\"text-align:justify\"><img alt=\"\" src=\"/packages/wysiwyg/ckeditor/plugins/kcfinder/upload/images/1.png\" style=\"height:271px; width:237px\" /></p>\r\n\r\n<ul>\r\n\t<li style=\"text-align:justify\">પ્રથમ ઈંડું મુક્વાની સરેરાશ ઉંમર:૧૪૨ દિવસ</li>\r\n\t<li style=\"text-align:justify\">સરેરાશ વાર્ષિક ઈંડા ઉત્પાદન : ૩૦૦ ઈંડા</li>\r\n\t<li style=\"text-align:justify\">૪૦ અઠવાડીયાની ઉંમરે ઈંડાનું સરેરાશ વજન : ૫૨ ગ્રામ</li>\r\n\t<li style=\"text-align:justify\">૭૨ અઠવાડીયાની ઉંમરે ઈંડાનું સરેરાશ વજન : ૫૪ ગ્રામ</li>\r\n\t<li style=\"text-align:justify\">સારી જીવાદોરી</li>\r\n</ul>\r\n",
      "mobile_detail": "<p style=\"text-align:justify\"><img alt=\"\" src=\"/packages/wysiwyg/ckeditor/plugins/kcfinder/upload/images/1.png\" style=\"height:271px; width:237px\" /></p>\r\n\r\n<ul>\r\n\t<li style=\"text-align:justify\">પ્રથમ ઈંડું મુક્વાની સરેરાશ ઉંમર:૧૪૨ દિવસ</li>\r\n\t<li style=\"text-align:justify\">સરેરાશ વાર્ષિક ઈંડા ઉત્પાદન : ૩૦૦ ઈંડા</li>\r\n\t<li style=\"text-align:justify\">૪૦ અઠવાડીયાની ઉંમરે ઈંડાનું સરેરાશ વજન : ૫૨ ગ્રામ</li>\r\n\t<li style=\"text-align:justify\">૭૨ અઠવાડીયાની ઉંમરે ઈંડાનું સરેરાશ વજન : ૫૪ ગ્રામ</li>\r\n\t<li style=\"text-align:justify\">સારી જીવાદોરી</li>\r\n</ul>\r\n",
      "created_at": "2014-05-03 17:11:43",
      "updated_at": "2014-05-11 13:41:31",
      "deleted": "0",
      "images": [],
      "videos": []
    }]
  }]
}

我有以下代码:

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    InputStream is=null;
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        //HttpPost httpPost = new HttpPost(url);
        HttpGet httpPost = new HttpGet(url);
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setHeader("X-Requested-With", "XMLHttpRequest");
        httpPost.setHeader("Mobile-Tokon", "7c^4N:9Y*Tq;P^f");
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    String json="";
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8000);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    JSONObject jObj=null;
    try {
        jObj = new JSONObject(json);
        Log.d("JSON",jObj.toString());
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

在我的 Java 类中,我这样调用上面的函数:

getJSONFromUrl("http://ikishan.192.168.1.87.xip.io/api/newapps/27?email=api.ikisan@aau.in&password=%~?7ON9Xjp;BcYu");

我得到一个错误:

java.lang.IllegalArgumentException: Invalid % sequence: %~? in query at index 83: http://ikishan.192.168.1.87.xip.io/api/newapps/27?email=api.ikisan@aau.in&password=%~?7ON9Xjp;BcYu

在这一行:

HttpGet httpPost = new HttpGet(url);

知道如何解决这个问题吗?

编辑

image

最佳答案

那是因为您提到的 URL 不是 this 中指定的有效 URL所以回答。

a complete URL is always in its encoded form: you take the strings for the individual components (scheme, authority, path, etc.), encode each according to its own rules, and then combine them into the complete URL string. Trying to build a complete unencoded URL string and then encode it separately leads to subtle bugs, like spaces in the path being incorrectly changed to plus signs (which an RFC-compliant server will interpret as real plus signs, not encoded spaces).

IllegalArgumentException异常中提到的那些无效字符不应该直接用GET请求发送,需要转换成这种格式

Character   From Windows-1252   From UTF-8
  %               %25               %25
  ~               %7E               %7E
  ?               %3F               %3F

参见 this完整列表的网址


在 Java 中,构建 URL 的正确方法是使用 URI 类。

import java.net.URLEncoder;

public class HelloWorld {
    static String uri = "http://ikishan.192.168.1.87.xip.io/api/newapps/27";
    static String params = "?email=api.ikisan@aau.in&password=%~?7ON9Xjp;BcYu";

     public static void main(String []args){
        try {
            String encodedParams = URLEncoder.encode( params, "ASCII" ).toString();
            System.out.println(uri + encodedParams);
        }
        catch(java.io.UnsupportedEncodingException uee) {
            //do something
        }
     }
}

输出

http://ikishan.192.168.1.87.xip.io/api/newapps/27%3Femail%3Dapi.ikisan%40aau.in%26password%3D%25%7E%3F7ON9Xjp%3BBcYu

关于java - 带有 header 参数的Get方法无法正常工作android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38626950/

相关文章:

java - 标准API中有没有可以深度克隆列表的方法?

java - 运算符优先级算法

android - onPageFinished 有时不调用 Android WebView

java - 获取 Java 中的原始字符串,在遗留应用程序中以未知格式编码

python - Werkzeug 引发 BrokenFilesystemWarning

ruby - String#encode 未修复 "invalid byte sequence in UTF-8"错误

java - 以编程方式在linux服务器上安装java

java - 为什么我得到 java.lang.AbstractMethodError : org. apache.poi.xssf.usermodel.XSSFCellStyle.getFillBackgroundColorColor()?

android - 如何为 API 21/Lollipop 以下的设备实现 setZ()?

android - 如何在android中显示包含来自另一个 Activity 的用户名的欢迎消息?