I'm trying to parse JSON data which is coming from this url.
我试图解析JSON数据来自这个url。
But I am getting these errors:
但是我犯了这些错误:
03-27 16:48:21.019: E/Buffer Error(23717): Error converting result java.lang.NullPointerException
E/Buffer Error(23717):错误转换结果java.lang.NullPointerException。
03-27 16:48:21.059: E/JSON Parser(23717): Error parsing data org.json.JSONException: End of input at character 0 of
E/JSON解析器(23717):错误解析数据org.json。JSONException:在字符0处结束输入。
Wwhen I debug my code; getJsonFromUrl()
method returns null jobject. Here is the JSONParser class which I used. What's causing the error?
当我调试代码时;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;
}
}
I'm making the call for this method from another class using these lines. (url parameter is defined at top)
我正在用这些线从另一个类中调用这个方法。(url参数在顶部定义)
JSONParser jParser = new JSONParser();
final JSONObject jobject = jParser.getJSONFromUrl(url);
1 个解决方案
#1
1
You are trying to get the JSON content using a HTTP POST method instead of the appropiated GET method (W3schools.com GET vs.POST), modify your source code to simplify and fix your HTTP request
您尝试使用HTTP POST方法获得JSON内容,而不是使用appropiated get方法(W3schools.com get vs.POST),修改源代码以简化和修复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) {
....
}
#1
1
You are trying to get the JSON content using a HTTP POST method instead of the appropiated GET method (W3schools.com GET vs.POST), modify your source code to simplify and fix your HTTP request
您尝试使用HTTP POST方法获得JSON内容,而不是使用appropiated get方法(W3schools.com get vs.POST),修改源代码以简化和修复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) {
....
}