此方法向服务器发出get请求并以字符串形式返回JSON响应。
public static String getJSONString(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpUriRequest request = new HttpGet(url);
HttpResponse response = null;
try {
response = httpClient.execute(request);
String result = EntityUtils.toString(response.getEntity());
return result;
} catch (Exception e) {
e.printStackTrace();
}
return "none";
}
这是我的JSON回复
07-18 19:34:53.816: I/MyResponse(27454): {"status":"ok","count":1,"pages":1,"category":{"id":7,"slug":"%d8%af%d9%83%d8%aa%d9%88%d8%b1-%d9%85%d8%b5%d8%b7%d9%81%d9%89-%d9%85%d8%ad%d9%85%d9%88%d8%af","title":"\u062f\u0643\u062a\u0648\u0631 \u0645\u0635\u0637\u0641\u0649 \u0645\u062d\u0645\u0648\u062f","description":"","parent":0,"post_count":1},"posts":[{"id":39,"type":"post","slug":"%d9%83%d8%aa%d8%a7%d8%a8-%d8%a7%d9%84%d8%ae%d8%b1%d9%88%d8%ac-%d9%85%d9%86-%d8%a7%d9%84%d8%aa%d8%a7%d8%a8%d9%88%d8%aa-2","url":"http:\/\/ovtraining.net\/wpf\/%d9%83%d8%aa%d8%a7%d8%a8-%d8%a7%d9%84%d8%ae%d8%b1%d9%88%d8%ac-%d9%85%d9%86-%d8%a7%d9%84%d8%aa%d8%a7%d8%a8%d9%88%d8%aa-2\/","status":"publish","title":"\u0643\u062a\u0627\u0628: \u0627\u0644\u062e\u0631\u0648\u062c \u0645\u0646 \u0627\u0644\u062a\u0627\u0628\u0648\u062a","title_plain":"\u0643\u062a\u0627\u0628: \u0627\u0644\u062e\u0631\u0648\u062c \u0645\u0646 \u0627\u0644\u062a\u0627\u0628\u0648\u062a","content":"<p>\u0627\u0646 \u0627\u0644\u0633\u0639\u0627\u062f\u0629 \u0644\u064a\u0633\u062a \u062d\u0638\u0627 \u0648\u0644\u0627 \u0628\u062e\u062a\u0627 \u0648\u0625\u0646\u0645\u0627 \u0647\u064a \u0642\u062f\u0631\u0629\u060c \u0623\u0628\u0648\u0627\u0628 \u0627\u0644\u0633\u0639\u0627\u062f\u0629 \u0644\u0627 \u062a\u0641\u062a\u062d \u0625\u0644\u0627 \u0645\u0646 \u0627\u0644\u062f\u0627\u062e\u0644 \u0645\u0646 \u062f\u0627\u062e\u0644 \u0646\u0641\u0633\u0643\u060c \u0627\u0644\u0633\u0639\u0627\u062f\u0629 \u062a\u062c\u064a\u0626\u0643 \u0645\u0646 \u0627\u0644\u0637\u0631\u064a\u0642\u0629 \u0627\u0644\u062a\u064a \u062a\u0646\u0638\u0631 \u0628\u0647\u0627 \u0625\u0644\u0649 \u0627\u0644\u062f\u0646\u064a\u0627\u060c \u0648\u0645\u0646 \u0627\u0644\u0637\u0631\u064a\u0642\u0629 \u0627\u0644\u062a\u064a \u062a\u0633\u0644\u0643 \u0628\u0647\u0627 \u0633\u0628\u064a\u0644\u0643.<\/p>\n","excerpt":"<p>\u0627\u0646 \u0627\u0644\u0633\u0639\u0627\u062f\u0629 \u0644\u064a\u0633\u062a \u062d\u0638\u0627 \u0648\u0644\u0627 \u0628\u062e\u062a\u0627 \u0648\u0625\u0646\u0645\u0627 \u0647\u064a \u0642\u062f\u0631\u0629\u060c \u0623\u0628\u0648\u0627\u0628 \u0627\u0644\u0633\u0639\u0627\u062f\u0629 \u0644\u0627 \u062a\u0641\u062a\u062d \u0625\u0644\u0627 \u0645\u0646 \u0627\u0644\u062f\u0627\u062e\u0644 \u0645\u0646 \u062f\u0627\u062e\u0644 \u0646\u0641\u0633\u0643\u060c \u0627\u0644\u0633\u0639\u0627\u062f\u0629 \u062a\u062c\u064a\u0626\u0643 \u0645\u0646 \u0627\u0644\u0637\u0631\u064a\u0642\u0629 \u0627\u0644\u062a\u064a \u062a\u0646\u0638\u0631 \u0628\u0647\u0627 \u0625\u0644\u0649 \u0627\u0644\u062f\u0646\u064a\u0627\u060c \u0648\u0645\u0646 \u0627\u0644\u0637\u0631\u064a\u0642\u0629 \u0627\u0644\u062a\u064a \u062a\u0633\u0644\u0643 \u0628\u0647\u0627 \u0633\u0628\u064a\u0644\u0643.<\/p>\n","date":"2014-07-18 05:44:01","modified":"2014-07-18 05:44:01","categories":[{"id":7,"slug":"%d8%af%d9%83%d8%aa%d9%88%d8%b1-%d9%85%d8%b5%d8%b7%d9%81%d9%89-%d9%85%d8%ad%d9%85%d9%88%d8%af","title":"\u062f\u0643\u062a\u0648\u0631 \u0645\u0635\u0637\u0641\u0649 \u0645\u062d\u0645\u0648\u062f","description":"","parent":0,"post_count":1}],"tags":[],"author":{"id":1,"slug":"admin","name":"admin","first_name":"","last_name":"","nickname":"admin","url":"","description":""},"comments":[],"attachments":[],"comment_count":0,"comment_status":"open","thumbnail":"http:\/\/ovtraining.net\/wpf\/wp-content\/uploads\/2014\/07\/429145_10150655987184630_1108330336_n-672x372.jpg","custom_fields":{},"thumbnail_size":"post-thumbnail","thumbnail_images":{"full":{"url":"http:\/\/ovtraining.net\/wpf\/wp-content\/uploads\/2014\/07\/429145_10150655987184630_1108330336_n.jpg","width":960,"height":373},"thumbnail":{"url":"http:\/\/ovtraining.net\/wpf\/wp-content\/uploads\/2014\/07\/429145_10150655987184630_1108330336_n-150x150.jpg","width":150,"height":150},"medium":{"url":"http:\/\/ovtraining.net\/wpf\/wp-content\/uploads\/2014\/07\/429145_10150655987184630_1108330336_n-300x116.jpg","width":300,"height":116},"post-thumbnail":{"url":"http:\/\/ovtraining.net\/wpf\/wp-content\/uploads\/2014\/07\/429145_10150655987184630_1108330336_n-672x3
阿拉伯语回复如此“slug”:“%d8%af%d9%83%d8%aa%d9%88%d8%b1-%d9%85%d8%b5%d8%b7%d9%81 %D9%89-%D9%85%D8%AD%D9%85%D9%88%D8%AF” 我怎么能处理这个问题??
答案 0 :(得分:0)
一种方法是:
String result = EntityUtils.toString(response.getEntity());
String utf8String=new String(result.getBytes(),"UTF-8");
另一种方法是为您的请求设置标题,但我不确定。
答案 1 :(得分:0)
问题解决了
String in = "%d8%af%d9%83%d8%aa%d9%88%d8%b1";
String result = java.net.URLDecoder.decode(in, "UTF-8");