在后台服务中调用HttpGet但似乎不起作用

时间:2015-06-17 13:49:35

标签: android background android-service http-get

我正在尝试使用 HttpGet 发送值。该功能在服务中。我尝试的是当我点击活动发送此值时的按钮,以及当我的应用程序结束时。当应用程序打开时它正常工作我可以发送值并查看此值敬酒消息。当我结束应用程序时,我可以在Toast中看到值,但是当检查时我看到他们没有进入网页然后我的数据库。我的代码如下,

public class LocService extends Service {
Timer timer;
Handler handler;
Location location1;
String logId = "";

final static long TIME= 10000;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener locListener = new LocationListener() {
        @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onLocationChanged(Location loc) {
            location1 = new Location(loc);
            tempVeriler.loc = new Location(loc);//tempVeriler is a class keeps temp values
        }
    };
    boolean gps_enabled = locManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean network_enabled = locManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if (tempVeriler.id != null)
        logId = tempVeriler.id;

    if (gps_enabled) {
        locManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0, locListener);
    } else if (network_enabled) {
        locManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
    }
    timer= new Timer();
    handler = new Handler(Looper.getMainLooper());
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            giveInfo();
        }
    }, 0, TIME);

}

private void giveInfo() {
    handler.post(new Runnable() {
        @Override
        public void run() {
            if (location1 != null) {
                getInternetData();
                Toast.makeText(
                        LocService.this,
                        location1.getLatitude() + " "
                                + location1.getLongitude(),
                        Toast.LENGTH_SHORT).show();
            }
        }
    });
}

@Override
public void onDestroy() {
    timer.cancel();
    super.onDestroy();
}

public void getInternetData() {
    HttpClient client = new DefaultHttpClient();

    URI website;
    try {
        website = new URI(
                "http://www.someadress.com/Default.aspx?logID="
                        + logID+ "&latitude="
                        + new Double(location1.getLatitude()).toString()
                        + "&longitude="
                        + new Double(location1.getLongitude()).toString());

        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
    } catch (Exception e) {
    }
}

1 个答案:

答案 0 :(得分:-2)

您无法在UI或主线程中执行任何网络任务,您必须为此目的使用处理程序。以这种方式更改你的getInternetData()方法,

new Handler().postDelayed(new Runnable()
{
    @Override
    public void run() {
    HttpClient client = new DefaultHttpClient();

    URI website;
    try {
        website = new URI(
                "http://www.someadress.com/Default.aspx?logID="
                        + logID+ "&latitude="
                        + new Double(location1.getLatitude()).toString()
                        + "&longitude="
                        + new Double(location1.getLongitude()).toString());

        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
    } catch (Exception e) {
    }
  }
}, 500);

请阅读此NetworkOnMainThreadException | Android Developers