我正在尝试将一些 JSONObject 数据从我的 AsyncTask.OnPostExecute 传递回我的 MainActivity.JSONCallBackComplete。
我的问题是当我尝试检查我传回的 JSONObject 时,我得到以下执行:
10-14 18:40:25.820: E/AndroidRuntime(4153): FATAL EXCEPTION: main
10-14 18:40:25.820: E/AndroidRuntime(4153): java.lang.NullPointerException
10-14 18:40:25.820: E/AndroidRuntime(4153): at com.icerge.revivaltimes.MainActivity.JSONCallBackComplete(MainActivity.java:31)
10-14 18:40:25.820: E/AndroidRuntime(4153): at com.icerge.revivaltimes.JsonObj.onPostExecute(JsonObj.java:70)
10-14 18:40:25.820: E/AndroidRuntime(4153): at com.icerge.revivaltimes.JsonObj.onPostExecute(JsonObj.java:1)
10-14 18:40:25.820: E/AndroidRuntime(4153): at android.os.AsyncTask.finish(AsyncTask.java:417)
10-14 18:40:25.820: E/AndroidRuntime(4153): at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-14 18:40:25.820: E/AndroidRuntime(4153): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
10-14 18:40:25.820: E/AndroidRuntime(4153): at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 18:40:25.820: E/AndroidRuntime(4153): at android.os.Looper.loop(Looper.java:144)
10-14 18:40:25.820: E/AndroidRuntime(4153): at android.app.ActivityThread.main(ActivityThread.java:4937)
10-14 18:40:25.820: E/AndroidRuntime(4153): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 18:40:25.820: E/AndroidRuntime(4153): at java.lang.reflect.Method.invoke(Method.java:521)
10-14 18:40:25.820: E/AndroidRuntime(4153): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-14 18:40:25.820: E/AndroidRuntime(4153): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-14 18:40:25.820: E/AndroidRuntime(4153): at dalvik.system.NativeStart.main(Native Method)
我的 MainActivity 类如下:
public class MainActivity extends Activity{
private JSONObject jsonData = null;
public void JSONCallBackComplete(JSONObject jsonData){
this.jsonData = jsonData;
Log.e("TESTing in callback: ", jsonData.toString() );
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new JsonObj(this).execute("http://myserver/json");
}
}
我的 AsyncTask 类如下:
public class JsonObj extends AsyncTask<String, Void, JSONObject>{
MainActivity activity;
int tid;
String term;
public JsonObj(MainActivity activity){
this.activity = activity;
// Log.e("TESTING: ", activity.getClass().toString());
}
@Override
protected JSONObject doInBackground(String... url) {
// TODO Auto-generated method stub
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(url[0]);
JSONObject jsonObject = null;
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
result = sb.toString();
Log.e("JSON-Test [RESULT]: ", result);
jsonObject = new JSONObject(result);
} catch (Exception e) {
Log.e("JSON-Test [exception]: ", e.toString());
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return jsonObject;
}
@Override
protected void onPostExecute(JSONObject result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if( !result.equals(null) ){
this.activity.JSONCallBackComplete(result);
}
}
}
我在 AsyncTask 中有一个构造函数,它分配 MainActivity-Context 并使用它使用 JSONObject 参数调用我的 JSONCallBackComplete 反馈函数。
但是,我没有显示我的 JSON-DATA,而是得到了上面粘贴的异常。