我正在尝试通过调用webservices来获取数据,因为我正在使用Async任务类。正如我在屏幕截图中所说的那样,我成功地获得了json响应。但它在listener.onTaskCompletedObject(responseJson)中崩溃;在onPostExecute()中。 任何人都可以发现这对我不起作用的原因。
Activity.java
new AddressAsyncTask(getBaseContext(), new OnTaskCompletedObject() {
@Override
public void onTaskCompletedObject(JSONObject responseJson) {
Constants.dataAddress = responseJson.toString();
loaddata();
}
}).execute(email);
OnTaskCompletedObject.java
public interface OnTaskCompletedObject {
void onTaskCompletedObject(JSONObject responseJson);
}
AddressAsyncTask.java
public class AddressAsyncTask extends AsyncTask<String, Integer, JSONObject> {
private OnTaskCompletedObject listener;
private JSONObject responseJson = null;
private Context contxt;
private Activity activity;
String email;
public AddressAsyncTask(Context context, OnTaskCompletedObject onTaskCompletedObject) {
this.contxt = context;
}
// async task to accept string array from context array
@Override
protected JSONObject doInBackground(String... params) {
String path = null;
String response = null;
HashMap<String, String> request = null;
JSONObject requestJson = null;
DefaultHttpClient httpClient = null;
HttpPost httpPost = null;
StringEntity requestString = null;
ResponseHandler<String> responseHandler = null;
// get the email and password
Log.i("Email", params[0]);
try {
path = "http://xxxxxxxxxxxxxxxxx/MemberDetails";
new URL(path);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
// set the API request
request = new HashMap<String, String>();
request.put(new String("Email"), params[0]);
request.entrySet().iterator();
// Store locations in JSON
requestJson = new JSONObject(request);
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(path);
requestString = new StringEntity(requestJson.toString());
// sets the post request as the resulting string
httpPost.setEntity(requestString);
httpPost.setHeader("Content-type", "application/json");
// Handles the response
responseHandler = new BasicResponseHandler();
response = httpClient.execute(httpPost, responseHandler);
responseJson = new JSONObject(response);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
responseJson = new JSONObject(response);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return responseJson;
}
@Override
protected void onPostExecute(JSONObject result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
listener.onTaskCompletedObject(responseJson);
}
}
例外的屏幕截图
jsonresponse的截图
答案 0 :(得分:3)
甚至在从json获取数据后出现NullPointerException异常 响应
因为listener
是null
。
将listener
初始化为:
public AddressAsyncTask(Context context, OnTaskCompletedObject listener) {
this.contxt = context;
this.listener=objListener;
}
答案 1 :(得分:1)
尝试更改
listener.onTaskCompletedObject(responseJson);
到
listener.onTaskCompletedObject(result);
答案 2 :(得分:0)
您忘记在异步任务构造函数中初始化侦听器。
答案 3 :(得分:0)
您忘记在AddressAsyncTask构造函数中初始化您的侦听器。
public AddressAsyncTask(Context context, OnTaskCompletedObject onTaskCompletedObject) {
this.contxt = context;
this.listener=onTaskCompletedObject;
}