0
我正在學習Android中的JSON解析。我的logcat顯示NullPointerException onpostexecute方法。我已經分享了下面的代碼。在主要活動JSON解析Android中的NullPointerException異常
JSONParser jParser = (JSONParser) new JSONParser().execute(url);
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
// TODO: handle exception
e.printStackTrace();
}
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl();
protected String doInBackground(String... urls)
{
// TODO Auto-generated method stub
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url1);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
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");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return result;
}
protected void onPostExecute(String result) {
// try parse the string to a JSON object
try {
jObj = new JSONObject(result);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
}
public JSONObject getJSONFromUrl()
{
return jObj;
}
JSONParser對象我想不通的問題。提供建議....
檢查您是否得到回覆。如果不通過嘗試n catch –
我相信你是在拋出一個異常,同時試圖捕捉不同的異常... – Sam
onPostExecute意味着可能是你的結果爲空 – Raj