[英]Issue in making http call in android due to String format - illegal character in query at index 81
在我的應用程序中,我必須根據用戶選擇的TYPE和SUB TYPE從JSON服務獲取數據,並將數據顯示給用戶。
我知道如何發出http請求,如何解析響應,但是URL造成了問題。
網址是這樣的:
http://abc.abc.ab.abc/myappnameapiii/index.php/Api/getAllInfoctoryUser_type?data={"type":"TYP","sub_type":"SUB"}
我嘗試使用StringEscapeUtils
轉義"
。但這沒有幫助。
樣例代碼:
String baseUrl = "http://abc.abc.ab.abc/myappnameapiii/index.php/Api/getAllInfoctoryUser_type?data={";
String typeInfo = "\"type\":\""+selectedType+"\",\"sub_type\":\""+selectedSubType+"\"}";
typeInfo = StringEscapeUtils.escapeJava(typeInfo);
String finalUrl = baseUrl + typeInfo;
//Call asynctask using finalUrl
....
....
我發出http請求時遇到此異常:
java.lang.IllegalArgumentException: Illegal character in query at index 81: http://abc.abc.ab.abc/myappnameapiii/index.php/Api/getAllInfoctoryUser_type?data={\"type\":\"TYP\",\"sub_type\":\"SUB\"}
這是我嘗試解決的問題:
第一:
我在像這樣在AsyncTask中進行調用之前就嘗試了轉義typeInfo的typeInfo
:
try {
String finalUrl = baseUrl.concat(typeInfo);*/
String finalUrl = StringEscapeUtils.unescapeJava(url[0]);
object = RestJsonClient.connect(finalUrl);
} catch (Exception e) {
e.printStackTrace();
}
這沒有幫助。 它將異常拋出為:
java.lang.IllegalArgumentException: Illegal character in query at index 81: http://abc.abc.ab.abc/myappnameapiii/index.php/Api/getAllInfoctoryUser_type?data={"type":"TYP","sub_type":"SUB"}
第二:
提到這個問題,我根據帖子上的答案嘗試使用UTF-8編碼。
程式碼片段:
String baseUrl = url[0];
String typeInfo = URLEncoder.encode(url[1], "UTF-8");
String finalUrl = baseUrl.concat(typeInfo);
//String finalUrl = StringEscapeUtils.unescapeJava(url[0]);
object = RestJsonClient.connect(finalUrl);
在這里,我使用兩個String而不是一個finalUrl字符串將URL傳遞給asynctask。
這也會引發異常,例如:
java.lang.IllegalArgumentException: Illegal character in query at index 81: http://abc.abc.ab.abc/myappnameapiii/index.php/Api/getAllInfoctoryUser_type?data={%5C%22type%5C%22%3A%5C%22MANUFACTURER%5C%22%2C%5C%22sub_type%5C%22%3A%5C%22GOLD%5C%22%7D
我對現在該做什么一無所知。 如果有人可以幫助,那就太好了。
PS:相同的URL在瀏覽器中顯示結果。 網址正確,並且服務器上有數據。
編輯
根據@Tejas的評論,我嘗試將數據傳遞到POST請求的主體中,如下所示:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("type", "TYP"));
params.add(new BasicNameValuePair("sub_type", "SUB"));
//request.setEntity(new UrlEncodedFormEntity(params));
object = RestJsonClient.post( baseUrl, params);
RestJsonClient.post
方法的代碼在我的其他調用中運行良好。 所以我沒有在這里發布所有代碼。 得到響應后,該方法的代碼段為:
HttpEntity httpentity = response.getEntity();
if (httpentity != null) {
InputStream instream = httpentity.getContent();
String result = convertStreamToString(instream);
System.out.println("result: " + result);
json = new JSONObject(result);
instream.close();
}
現在沒有錯誤。 我從服務獲得狀態碼為OK(200)的響應。 但結果沒有數據。
08-10 10:33:42.141 7003-9831/jbc.gss.com.jewelnet I/System.out: response in restjson claas- org.apache.http.message.BasicHttpResponse@27ee4f83
08-10 10:33:42.141 7003-9831/pkg.name V/response code: 200
08-10 10:33:42.142 7003-9831/pkg.name I/System.out: result: {"response":"failure","message":" Not data available"}
08-10 10:33:42.150 7003-7003/pkg.name W/System.err: org.json.JSONException: No value for data
如果需要,我無法請求更改服務。 有人可以幫助我在GET請求中正確傳遞參數嗎?
使用URLEncoder
對URL參數字符串進行編碼。
編碼您的typeInfo
及其前面的{
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.