AndroidでJSON解析を学んでいます。私のlogcatはNullPointerException onpostexecuteメソッドを示しています。以下のコードを共有しました。
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 オブジェクト
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();
問題がわかりません。提案を提供します....