我正在尝试从 url 获取 json 字符串,当我使用这行代码时,我的方法返回一个空字符串值:
String jsonStr = getJsonStringFromURL(url);
这是我正在使用的方法:
public static String getJsonStringFromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jsonObject = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch (Exception e) {
return null;
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}
catch(Exception e) {
return null;
}
return result;
}
我有一个 url 变量,当我将 url 复制并粘贴到浏览器中时,它会返回并显示一个 json 字符串。任何建议或帮助将不胜感激。