public static String getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(url);
httppost.setHeader(
"Accept",
"text/html,application/xhtml+xml,application/xml,application/json;q=0.9,/;q=0.8");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return result;
}
这是我的代码我正在使用Httpget方法进行json解析当我尝试解析然后它显示org.json.jsonexception在android的字符0输入结束时也在结果空白值我得到的时候我从小提琴我得到回应我不知道在哪里做错了请帮助我并建议在哪里做错了。
答案 0 :(得分:0)
在设备浏览器中打开你的URl网址,如果它会给出响应,那么你的服务是好的,如果它不会给出respnse,那么在服务器中没有给出响应时,你的代码就会丢失。