1

我想将数据从android发送到服务器,在这里,我使用服务调用senddata AsyncTask。但我的应用程序出错了。你能帮帮我吗!!源服务

public class GpsService extends Service  {
HttpClient httpclient;
HttpPost httppost;
List<NameValuePair> nameValuePairs;
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    Toast.makeText(this, "Hello, Service dang chuan bi start!",  Toast.LENGTH_SHORT).show();
}
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Toast.makeText(this, "Hello, Service dang chuan bi stop!",  Toast.LENGTH_SHORT).show();
}
@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    //super.onStart(intent, startId);
    //doTimerTask();
    // Location initiation
    new senddata().execute();

}


public class senddata extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progressdialog;

    @Override
    protected Void doInBackground(Void... params) {

        try {
            Calendar cal=Calendar.getInstance();
            int date=cal.get(Calendar.DAY_OF_MONTH);
            int month=cal.get(Calendar.MONTH)+1;
            int year=cal.get(Calendar.YEAR);
            int hour=cal.get(Calendar.HOUR_OF_DAY);
            int min=cal.get(Calendar.MINUTE);
            String str1=hour+":"+min+" "+date+"/"+month+"/"+year;
            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://longvansolution.tk/gps.php");
            // add your data
            nameValuePairs = new ArrayList<NameValuePair>(3);
            nameValuePairs.add(new BasicNameValuePair("la", "test1"));
            nameValuePairs.add(new BasicNameValuePair("lon", "test2"));
            nameValuePairs.add(new BasicNameValuePair("str", str1));
            HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);
            httppost.addHeader(entity.getContentType());
            httppost.setEntity(entity);
            response = httpclient.execute(httppost);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(final Void unused) {
        // TODO Auto-generated method stub
        this.progressdialog.dismiss();

    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        //this.progressdialog = ProgressDialog.show(GpsService.this, "",
        //      "Loading...");
    }

}

}

这个错误

11-16 10:57:09.905: E/AndroidRuntime(1846): FATAL EXCEPTION: main

11-16 10:57:09.905: E/AndroidRuntime(1846): java.lang.NullPointerException

11-16 10:57:09.905: E/AndroidRuntime(1846): at LongVan.mapfinish.GpsService$senddata.onPostExecute(GpsService.java:153)

11-16 10:57:09.905: E/AndroidRuntime(1846):     at LongVan.mapfinish.GpsService$senddata.onPostExecute(GpsService.java:1)

11-16 10:57:09.905: E/AndroidRuntime(1846):     at android.os.AsyncTask.finish(AsyncTask.java:602)

11-16 10:57:09.905: E/AndroidRuntime(1846): at android.os.AsyncTask.access$600(AsyncTask.java:156)

11-16 10:57:09.905: E/AndroidRuntime(1846): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)

11-16 10:57:09.905: E/AndroidRuntime(1846):     at android.os.Handler.dispatchMessage(Handler.java:99)

11-16 10:57:09.905: E/AndroidRuntime(1846): at android.os.Looper.loop(Looper.java:137)

11-16 10:57:09.905: E/AndroidRuntime(1846): at android.app.ActivityThread.main(ActivityThread.java:4514)

11-16 10:57:09.905: E/AndroidRuntime(1846): at java.lang.reflect.Method.invokeNative(Native Method)

11-16 10:57:09.905: E/AndroidRuntime(1846): at java.lang.reflect.Method.invoke(Method.java:511)

11-16 10:57:09.905: E/AndroidRuntime(1846): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)

11-16 10:57:09.905: E/AndroidRuntime(1846): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)

11-16 10:57:09.905: E/AndroidRuntime(1846): at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

progressdialog 对象未初始化

试试这个代码

   public class GpsService extends Service  {
    HttpClient httpclient;
    HttpPost httppost;
    List<NameValuePair> nameValuePairs;
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Toast.makeText(this, "Hello, Service dang chuan bi start!",  Toast.LENGTH_SHORT).show();
    }
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "Hello, Service dang chuan bi stop!",  Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        //super.onStart(intent, startId);
        //doTimerTask();
        // Location initiation
        new senddata().execute();

    }


    public class senddata extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progressdialog;

        @Override
        protected Void doInBackground(Void... params) {

            try {
                Calendar cal=Calendar.getInstance();
                int date=cal.get(Calendar.DAY_OF_MONTH);
                int month=cal.get(Calendar.MONTH)+1;
                int year=cal.get(Calendar.YEAR);
                int hour=cal.get(Calendar.HOUR_OF_DAY);
                int min=cal.get(Calendar.MINUTE);
                String str1=hour+":"+min+" "+date+"/"+month+"/"+year;
                httpclient = new DefaultHttpClient();
                httppost = new HttpPost("http://longvansolution.tk/gps.php");
                // add your data
                nameValuePairs = new ArrayList<NameValuePair>(3);
                nameValuePairs.add(new BasicNameValuePair("la", "test1"));
                nameValuePairs.add(new BasicNameValuePair("lon", "test2"));
                nameValuePairs.add(new BasicNameValuePair("str", str1));
                HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);
                httppost.addHeader(entity.getContentType());
                httppost.setEntity(entity);
                response = httpclient.execute(httppost);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(final Void unused) {
            // TODO Auto-generated method stub
            progressdialog .dismiss();

        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            progressdialog = new ProgressDialog(GpsService.this);
            progressdialog.setMessage("Loading...");
            progressdialog.setCancelable(false);
            progressdialog.show();

        }

    }
于 2012-11-16T05:10:44.933 回答