我在 doinbackground 中使用 httppost 方法,我也得到了响应。现在,当我将数据传递给 web 服务时,我得到了一个必须解析的 Jsonobject。并且该 jsonobject 存储在下面的 responsebody 中。我已将返回语句设置为“res”。但在 onpost 执行中我得到一个空指针异常。
我想在 onpostexecute 方法中使用 String responseBody 吗?
class Thread extends AsyncTask<String, Void , String>{
private String responseBody;
private String res;
@Override
protected String doInBackground(String... params) {
HttpClient client = new DefaultHttpClient();
HttpResponse response;
JSONObject json=new JSONObject();
HttpPost post = new HttpPost(url1);
try {
json.put("URL",getqrcode());
json.put("EmailID", getuseremail());
StringEntity stringEntity = new StringEntity(json.toString());
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
post.setEntity(stringEntity);
response = client.execute(post);
Log.e("RESPONSE", response.toString());
String responseBody = EntityUtils
.toString(response.getEntity());
String res= responseBody.toString();
Log.e("RESPONSE BODY", responseBody);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
return res;
}
@Override
protected void onPostExecute(String res) {
Log.e("response is", res);
// TODO Auto-generated method stub
super.onPostExecute(res);
}