用于将GPS坐标发送到服务器的Android应用程序崩溃

时间:2014-01-21 11:59:29

标签: android android-intent android-asynctask gps

我正在构建一个Android应用程序,它将GPS坐标发送到服务器,但它显示gps坐标,之后立即崩溃,而不是将坐标发送到服务器。

我的MainActivity.java是

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends Activity implements LocationListener{


private TextView latituteField;
  private TextView longitudeField;
  private LocationManager locationManager;
  private String provider;
  String lat;
String lng;


/** Called when the activity is first created. */

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    latituteField = (TextView) findViewById(R.id.TextView02);
    longitudeField = (TextView) findViewById(R.id.TextView04);

    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use
    // default
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    // Initialize the location fields
    if (location != null) {
      System.out.println("Provider " + provider + " has been selected.");
      onLocationChanged(location);
    } else {
      latituteField.setText("Location not available");
      longitudeField.setText("Location not available");
    }
  }

  /* Request updates at startup */
  @Override
  protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider, 400, 1, this);
  }

  /* Remove the locationlistener updates when Activity is paused */
  @Override
  protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
  }




  @Override
  public void onLocationChanged(Location location) {
     lat = Double.toString(location.getLatitude());
     lng = Double.toString (location.getLongitude());
    latituteField.setText(String.valueOf(lat));
    longitudeField.setText(String.valueOf(lng));
    longitudeField.setText(String.valueOf(lng));
    LoadServerASYNC task = new LoadServerASYNC();
    task.execute();


       }



  @Override
  public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

  }

  @Override
  public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
        Toast.LENGTH_SHORT).show();

  }

  @Override
  public void onProviderDisabled(String provider) {
    Toast.makeText(this, "Disabled provider " + provider,
        Toast.LENGTH_SHORT).show();
  }

  private class LoadServerASYNC extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... arg0) {
        postData(lat,lng);
         StringBuilder builder = new StringBuilder();
          HttpClient client = new DefaultHttpClient();
        return null;
    }

    public void postData(String currLat, String currLon) { 
        // List with arameters and their values

        String Text2 = "String is: " +

        "Latitud = " + currLat +

        "Longitud = " + currLon;

        Toast.makeText(getApplicationContext(), Text2, Toast.LENGTH_LONG).show();

        HttpPost post = new HttpPost("http://www.url.com/*.php");
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("myusername", currLat));
        nameValuePairs.add(new BasicNameValuePair("mypassword", currLon));
        HttpClient client = new DefaultHttpClient();    

        try {
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            String responseText = EntityUtils.toString(entity);
            responseText = responseText.trim();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        }
         }
         }

这是我的logcat

 01-21 06:16:17.400: I/System.out(1107): Provider gps has been selected.
    01-21 06:16:17.630: D/gralloc_goldfish(1107): Emulator without GPU emulation detected.
    01-21 06:16:17.790: W/dalvikvm(1107): threadid=11: thread exiting with uncaught       exception (group=0xb4a15ba8)
    01-21 06:16:17.820: E/AndroidRuntime(1107): FATAL EXCEPTION: AsyncTask #1
    01-21 06:16:17.820: E/AndroidRuntime(1107): Process: com.example.gpsexample2a, PID: 1107
    01-21 06:16:17.820: E/AndroidRuntime(1107): java.lang.RuntimeException: An error   occured while executing doInBackground()
    01-21 06:16:17.820: E/AndroidRuntime(1107):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
    01-21 06:16:17.820: E/AndroidRuntime(1107):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    01-21 06:16:17.820: E/AndroidRuntime(1107):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    01-21 06:16:17.820: E/AndroidRuntime(1107):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    01-21 06:16:17.820: E/AndroidRuntime(1107):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    01-21 06:16:17.820: E/AndroidRuntime(1107):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    01-21 06:16:17.820: E/AndroidRuntime(1107):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    01-21 06:16:17.820: E/AndroidRuntime(1107):     at java.lang.Thread.run(Thread.java:841)
    01-21 06:16:17.820: E/AndroidRuntime(1107): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    01-21 06:16:17.820: E/AndroidRuntime(1107):     at android.os.Handler.<init>(Handler.java:200)
    01-21 06:16:17.820: E/AndroidRuntime(1107):     at android.os.Handler.<init>(Handler.java:114)
    01-21 06:16:17.820: E/AndroidRuntime(1107):     at android.widget.Toast$TN.<init>(Toast.java:327)
    01-21 06:16:17.820: E/AndroidRuntime(1107):     at android.widget.Toast.<init>(Toast.java:92)
    01-21 06:16:17.820: E/AndroidRuntime(1107):     at android.widget.Toast.makeText(Toast.java:241)
   01-21 06:16:17.820: E/AndroidRuntime(1107):  at com.example.gpsexample2a.MainActivity$LoadServerASYNC.postData(MainActivity.java:143)
   01-21 06:16:17.820: E/AndroidRuntime(1107):  at   com.example.gpsexample2a.MainActivity$LoadServerASYNC.doInBackground(MainActivity.java:128)
   01-21 06:16:17.820: E/AndroidRuntime(1107):  at com.example.gpsexample2a.MainActivity$LoadServerASYNC.doInBackground(MainActivity.java:1) 
   01-21 06:16:17.820: E/AndroidRuntime(1107):  at android.os.AsyncTask$2.call(AsyncTask.java:288) 
   01-21 06:16:17.820: E/AndroidRuntime(1107):  at java.util.concurrent.FutureTask.run(FutureTask.java:237)

4 个答案:

答案 0 :(得分:2)

从postData方法中删除此行

Toast.makeText(getApplicationContext(), Text2, Toast.LENGTH_LONG).show();

您无法在doInBackground(String... arg0)

中调用Toast方法

或使用Handler

答案 1 :(得分:1)

你应该使用

runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(),data, Toast.LENGTH_LONG).show();
                }
            });

答案 2 :(得分:1)

 Toast.makeText(getApplicationContext(), Text2, Toast.LENGTH_LONG).show();
postData方法中的

是原因。你不应该在AsyncTask类“doInBackground()”方法上执行UI-Task。 但你可以从其他方法显示Toast,如“onPreExecute()”,“onProgressUpdate()”,“onPostExecute()”方法。要知道,如何正确使用AyncTask类参考this link

答案 3 :(得分:0)

问题在于这行代码:

Toast.makeText(getApplicationContext(), Text2, Toast.LENGTH_LONG).show();

此代码与UI比赛相关,Android想要在本次比赛中运行,而不是在后台线程中运行。

因此有这个日志:

  

异常:无法在未调用Looper.prepare()

的线程内创建处理程序