0

我有一个应用程序需要在位置更改时将当前用户位置发送到远程 URL。我正在使用以下代码使用一些参数调用 url。但是下面的代码只能工作一次,当我第二次不能工作时,什么也没有发生。我在服务器日志上看到一个请求,然后没有收到请求。

public String sendRequest(String url){
  InputStream content = null;
      try {
        HttpResponse response = httpclient.execute(new HttpGet(url));
        response.setHeader("Cache-Control", "no-cache");
        content = response.getEntity().getContent();
        content.close();
      } catch (Exception e) {
        try {
            content.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


      }
        return content;
}
  • 向 url 发送多个请求的最佳方式是什么?
  • 为什么我不能使用上面的代码发送多个请求?

注意:我正在使用模拟器并手动发送位置。

这是错误日志,当我第二次尝试调用该方法时

03-30 16:48:15.205: E/ActivityThread(645): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ceef60 that was originally bound here
03-30 16:48:15.205: E/ActivityThread(645): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ceef60 that was originally bound here
03-30 16:48:15.205: E/ActivityThread(645):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
03-30 16:48:15.205: E/ActivityThread(645):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
03-30 16:48:15.205: E/ActivityThread(645):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
03-30 16:48:15.205: E/ActivityThread(645):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
03-30 16:48:15.205: E/ActivityThread(645):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
03-30 16:48:15.205: E/ActivityThread(645):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
03-30 16:48:15.205: E/ActivityThread(645):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
03-30 16:48:15.205: E/ActivityThread(645):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
03-30 16:48:15.205: E/ActivityThread(645):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
03-30 16:48:15.205: E/ActivityThread(645):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
03-30 16:48:15.205: E/ActivityThread(645):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
03-30 16:48:15.205: E/ActivityThread(645):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
03-30 16:48:15.205: E/ActivityThread(645):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-30 16:48:15.205: E/ActivityThread(645):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-30 16:48:15.205: E/ActivityThread(645):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-30 16:48:15.205: E/ActivityThread(645):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-30 16:48:15.205: E/ActivityThread(645):  at java.lang.Thread.run(Thread.java:856)
03-30 16:48:15.225: E/StrictMode(645): null
03-30 16:48:15.225: E/StrictMode(645): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ceef60 that was originally bound here
03-30 16:48:15.225: E/StrictMode(645):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
03-30 16:48:15.225: E/StrictMode(645):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
03-30 16:48:15.225: E/StrictMode(645):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
03-30 16:48:15.225: E/StrictMode(645):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
03-30 16:48:15.225: E/StrictMode(645):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
03-30 16:48:15.225: E/StrictMode(645):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
03-30 16:48:15.225: E/StrictMode(645):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
03-30 16:48:15.225: E/StrictMode(645):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
03-30 16:48:15.225: E/StrictMode(645):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
03-30 16:48:15.225: E/StrictMode(645):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
03-30 16:48:15.225: E/StrictMode(645):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
03-30 16:48:15.225: E/StrictMode(645):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
03-30 16:48:15.225: E/StrictMode(645):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-30 16:48:15.225: E/StrictMode(645):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-30 16:48:15.225: E/StrictMode(645):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-30 16:48:15.225: E/StrictMode(645):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-30 16:48:15.225: E/StrictMode(645):  at java.lang.Thread.run(Thread.java:856)
03-30 16:48:15.225: W/ActivityManager(275): Unbind failed: could not find connection for android.os.BinderProxy@4100e3e8
03-30 16:48:15.246: E/ActivityThread(645): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d2cc28 that was originally bound here
03-30 16:48:15.246: E/ActivityThread(645): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d2cc28 that was originally bound here
03-30 16:48:15.246: E/ActivityThread(645):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
03-30 16:48:15.246: E/ActivityThread(645):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
03-30 16:48:15.246: E/ActivityThread(645):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
03-30 16:48:15.246: E/ActivityThread(645):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
03-30 16:48:15.246: E/ActivityThread(645):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
03-30 16:48:15.246: E/ActivityThread(645):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
03-30 16:48:15.246: E/ActivityThread(645):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
03-30 16:48:15.246: E/ActivityThread(645):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
03-30 16:48:15.246: E/ActivityThread(645):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
03-30 16:48:15.246: E/ActivityThread(645):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
03-30 16:48:15.246: E/ActivityThread(645):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
03-30 16:48:15.246: E/ActivityThread(645):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-30 16:48:15.246: E/ActivityThread(645):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-30 16:48:15.246: E/ActivityThread(645):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-30 16:48:15.246: E/ActivityThread(645):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-30 16:48:15.246: E/ActivityThread(645):  at java.lang.Thread.run(Thread.java:856)
03-30 16:48:15.265: E/StrictMode(645): null
03-30 16:48:15.265: E/StrictMode(645): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d2cc28 that was originally bound here
03-30 16:48:15.265: E/StrictMode(645):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
03-30 16:48:15.265: E/StrictMode(645):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
03-30 16:48:15.265: E/StrictMode(645):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
03-30 16:48:15.265: E/StrictMode(645):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
03-30 16:48:15.265: E/StrictMode(645):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
03-30 16:48:15.265: E/StrictMode(645):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
03-30 16:48:15.265: E/StrictMode(645):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
03-30 16:48:15.265: E/StrictMode(645):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
03-30 16:48:15.265: E/StrictMode(645):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
03-30 16:48:15.265: E/StrictMode(645):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)

我看到了问题,我必须添加

HttpGet getter =  new HttpGet(url);
getter.setHeader("Cache-Control", "no-cache");
4

1 回答 1

1

You're probably experiencing a caching issue. Android doesn't see any difference in your request and you're probably asking for it very close together.

add a HTTP header:

Cache-Control: no-cache

于 2013-03-30T14:30:54.347 回答