![](/img/trans.png)
[英]Error parsing data org.json.JSONException: End of input at character 0 of
[英]Error parsing data org.json.JSONException: End of input at character 0 of
我正在嘗試解析來自此url的 JSON數據。
但是我收到這些錯誤:
03-27 16:48:21.019:E / Buffer錯誤(23717):轉換結果java.lang.NullPointerException時出錯
03-27 16:48:21.059:E / JSON解析器(23717):解析數據org.json.JSON時出錯:字符0的輸入結束
當我調試代碼時; getJsonFromUrl()
方法返回null jobject 。 這是我使用的JSONParser類 。 是什么導致錯誤?
public class JSONParser {
static InputStream iStream = null;
static JSONArray jarray = null;
static JSONObject jObj= null;
static String json = "";
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
iStream.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parsing the string to a JSON object
try {
if (json != null) {
jObj = new JSONObject(json);
} else {
jObj = null;
}
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
我正在使用這些行從另一個類中對此方法進行調用。 (URL參數在頂部定義)
JSONParser jParser = new JSONParser();
final JSONObject jobject = jParser.getJSONFromUrl(url);
您嘗試使用HTTP POST方法而不是適當的GET方法( W3schools.com GET vs.POST )獲取JSON內容,請修改源代碼以簡化和修復HTTP請求
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(get);
String json = EntityUtils.toString(httpResponse.getEntity());
System.out.println(json);
....
....
} catch (Exception e) {
....
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.