我想将我的gps位置发送到服务器但它在doinbackground方法中显示错误,显示logcat经度,纬度和deviceid以及该应用程序崩溃后。 这个类显示了我的异步类。
private class MyAsyncTask extends AsyncTask<Void, Void, Void>
{
ProgressDialog mProgressDialog;
@Override
protected void onPostExecute(Void result) {
mProgressDialog.dismiss();
}
@Override
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(MainActivity.this, "Loading...", "Data is Loading...");
}
@Override
protected Void doInBackground(Void... params) {
HttpClient httpclient = new DefaultHttpClient();
GPSTracker gps = new GPSTracker(MainActivity.this);
String lat=Double.toString(gps.getLatitude());
String lon=Double.toString(gps.getLongitude());
String url="http://m4yank.com/inhouse/mhere/api/api.php?q=track&uid=" +MainActivity.getDeviceId(MainActivity.this)+"&lat=" + lat + "&longt=" + lon;
HttpPost httpost = new HttpPost("url");
Log.d("Latitude", ""+lat);
Log.d("Longitude", ""+lon);
Log.d("Device id", ""+MainActivity.getDeviceId(MainActivity.this));
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("latitude", lat));
nameValuePairs.add(new BasicNameValuePair("longitude", lon));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
//Log.d("Latitude", ""+lat);
// Log.d("Longitude", ""+lon);
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
在logcat中显示错误:
12-03 21:22:37.540: E/AndroidRuntime(2410): FATAL EXCEPTION: AsyncTask #1
12-03 21:22:37.540: E/AndroidRuntime(2410): java.lang.RuntimeException: An error occured while executing doInBackground()
12-03 21:22:37.540: E/AndroidRuntime(2410): at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-03 21:22:37.540: E/AndroidRuntime(2410): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
12-03 21:22:37.540: E/AndroidRuntime(2410): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
12-03 21:22:37.540: E/AndroidRuntime(2410): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
12-03 21:22:37.540: E/AndroidRuntime(2410): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-03 21:22:37.540: E/AndroidRuntime(2410): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-03 21:22:37.540: E/AndroidRuntime(2410): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-03 21:22:37.540: E/AndroidRuntime(2410): at java.lang.Thread.run(Thread.java:856)
12-03 21:22:37.540: E/AndroidRuntime(2410): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=url
12-03 21:22:37.540: E/AndroidRuntime(2410): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
12-03 21:22:37.540: E/AndroidRuntime(2410): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
12-03 21:22:37.540: E/AndroidRuntime(2410): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-03 21:22:37.540: E/AndroidRuntime(2410): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-03 21:22:37.540: E/AndroidRuntime(2410): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-03 21:22:37.540: E/AndroidRuntime(2410): at com.example.gps.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:251)
12-03 21:22:37.540: E/AndroidRuntime(2410): at com.example.gps.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:1)
12-03 21:22:37.540: E/AndroidRuntime(2410): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-03 21:22:37.540: E/AndroidRuntime(2410): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-03 21:22:37.540: E/AndroidRuntime(2410):
答案 0 :(得分:0)
应该有一条错误消息,说明错误发生的位置或位置。 我假设你在清单中设置了所有必需的权限......
答案 1 :(得分:0)
line 258: httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
然后httppost
为null
,因为您在这一行上获得了NullPointerException
。我没有看到httppost
定义在哪里,更不用说初始化了。