我尝试对发送用户名和密码以及Google地图中的某些圈子的服务器发送应该发布的内容。目前我收到了java.lang.NullPointerException。调试后我发现" response.getEntity();"返回null。此外,我得到一个IOException(来自具有"客户端"标记logcat)的catch。根据我的理解,我的实施应该是正确的,但是我没有注意到一个问题。任何想法是什么导致了这一点 logcat的:
07-29 14:23:30.253: E/AndroidRuntime(16810): FATAL EXCEPTION: AsyncTask #1
07-29 14:23:30.253: E/AndroidRuntime(16810): Process: com.example.maptest, PID: 16810
07-29 14:23:30.253: E/AndroidRuntime(16810): java.lang.RuntimeException: An error occured while executing doInBackground()
07-29 14:23:30.253: E/AndroidRuntime(16810): at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-29 14:23:30.253: E/AndroidRuntime(16810): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-29 14:23:30.253: E/AndroidRuntime(16810): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-29 14:23:30.253: E/AndroidRuntime(16810): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-29 14:23:30.253: E/AndroidRuntime(16810): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-29 14:23:30.253: E/AndroidRuntime(16810): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-29 14:23:30.253: E/AndroidRuntime(16810): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-29 14:23:30.253: E/AndroidRuntime(16810): at java.lang.Thread.run(Thread.java:841)
07-29 14:23:30.253: E/AndroidRuntime(16810): Caused by: java.lang.NullPointerException
07-29 14:23:30.253: E/AndroidRuntime(16810): at com.example.maptest.RegisterDialog$1$1SendSaveRequest.sendMessage(RegisterDialog.java:124)
07-29 14:23:30.253: E/AndroidRuntime(16810): at com.example.maptest.RegisterDialog$1$1SendSaveRequest.doInBackground(RegisterDialog.java:142)
07-29 14:23:30.253: E/AndroidRuntime(16810): at com.example.maptest.RegisterDialog$1$1SendSaveRequest.doInBackground(RegisterDialog.java:1)
07-29 14:23:30.253: E/AndroidRuntime(16810): at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-29 14:23:30.253: E/AndroidRuntime(16810): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-29 14:23:30.253: E/AndroidRuntime(16810): ... 4 more
我的代码:
saveCircles.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
class SendSaveRequest extends AsyncTask<String, Void, String> {
// private Gson gson = new GsonBuilder().create();
// String data = gson.toJson(message);
private String sendMessage(String message, String address) {
String url = "http://192.168.87.108:8080/MSS/"
+ address;
HttpPost post = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
1);
nameValuePairs.add(new BasicNameValuePair("report",
message));
try {
post.setEntity(new UrlEncodedFormEntity(
nameValuePairs));
} catch (UnsupportedEncodingException e) {
Log.i("client", "Your url encoding is shiat fail");
}
HttpClient client = new DefaultHttpClient();
HttpResponse response = null;
try {
Log.i("client", "Trying to execute");
response = client.execute(post);
Log.i("client", "Execution success");
} catch (ClientProtocolException e) {
Log.i("client", "Problems with execute post, ClientProtocolException");
e.printStackTrace();
} catch (IOException e) {
Log.i("client", "Problems with execute post, IOException");
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
String responseText = "";
try {
responseText = EntityUtils.toString(entity);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return responseText;
}
@Override
protected String doInBackground(String... params) {
return sendMessage(params[0], params[1]);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(mainActivityContext, result,
Toast.LENGTH_LONG).show();
}
}
String msg = gson.toJson(mainActivityContext
.generateSavePayload(
mainActivityContext.circlesToArea(),
mainActivityContext.userId,
mainActivityContext.userPassword));
new SendSaveRequest().execute(msg, "save");
}
});