我遇到了网址问题并获得了null作为响应。我也试图让它编码,但是reponce并没有按照预期得到..
这是我的代码:
try
{
String url = "http://mymemory.translated.net/api/get?q=Thanks&langpair=EN|KO" + query;
Log.i("URL", ">>>"+url);
JSONObject Responce = getJSONfromURL(url.toString());
Log.e("", "Responce"+Responce);
}
catch (Exception e) {}
错误日志:
10-12 20:13:35.411: INFO/URL(6844): >>>http://mymemory.translated.net/api/get?q=Thanks&langpair=EN|KO
10-12 20:13:35.531: ERROR/log_tag(6844): Error in http connection java.lang.IllegalArgumentException: Illegal character in query at index 59: http://mymemory.translated.net/api/get?q=Thanks&langpair=EN|KO
10-12 20:13:35.531: ERROR/log_tag(6844): Error converting result java.lang.NullPointerException
10-12 20:13:35.531: ERROR/log_tag(6844): Error parsing data org.json.JSONException: End of input at character 0 of
10-12 20:13:35.541: ERROR/(6844): Responcenull
编码后
网址编码方法适用..
String query = URLEncoder.encode("Thanks&langpair=EN|KO", "utf-8");
String url = "http://mymemory.translated.net/api/get?q=" + query;
Log.i("URL", ">>>"+url);
JSONObject Responce = getJSONfromURL(url.toString());
Log.e("", "Responce"+Responce);
记录::
10-12 20:21:01.180: INFO/URL(7050): >>>http://mymemory.translated.net/api/get?q=Thanks%26langpair%3DEN%7CKO
10-12 20:21:01.710: ERROR/(7050): Responce{"responseData":{"translatedText":"INVALID LANGUAGE PAIR SPECIFIED. EXAMPLE: LANGPAIR=EN|IT USING 2 LETTER ISO OR RFC3066 LIKE ZH-CN. ALMOST ALL LANGUAGES SUPPORTED BUT SOME MAY HAVE NO CONTENT"},"matches":"","responseStatus":403,"responseDetails":"INVALID LANGUAGE PAIR SPECIFIED. EXAMPLE: LANGPAIR=EN|IT USING 2 LETTER ISO OR RFC3066 LIKE ZH-CN. ALMOST ALL LANGUAGES SUPPORTED BUT SOME MAY HAVE NO CONTENT"}
答案 0 :(得分:1)
请勿在查询字符串中对“=”进行URLEncode。基本上,您只想对urlEncode值。因此,如果您的网址看起来像这样,那就可以了:
http://mymemory.translated.net/api/get?q=Thanks&langpair=EN%7CKO