2012-11-16 183 views
1

我想從android發送數據到服務器,在這裏,我使用服務調用senddata AsyncTask.But我的應用程序是錯誤的。可以幫我! 源服務發送數據到服務器使用服務在android

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) 
+1

快速查看說,你的程序控制essdialog對象不是初始化的。您已評論asynctask onPreExecute方法中的該部分。 –

回答

0

progressdialog對象不intialized

試試這個代碼

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(); 

     } 

    }