开发者

HTTP POST works in application but not in service

HTTP POST

My android application needs to do an HTTP post to a php script. The post needs to happen in a service. For some reason the POST gets a connection timeout when running in the service but if i copy the method to the onCreate event of my application and run, it works fine. I have no idea what could be different. This is my method

    String ur开发者_JAVA技巧l = "http://myurl/" +
  "?tmpl=javascript" +
  "&mode=recieved" + 
  "&id=" + id;
  URI uri = null;
  try {
   url = url.replace(" ", "%20");
   uri = new URI(url);
  } catch (URISyntaxException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  HttpGet httpGet = new HttpGet(uri);
  HttpParams httpParameters = new BasicHttpParams();
  // Set the timeout in milliseconds until a connection is established.
  int timeoutConnection = 10000;
  HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
  // Set the default socket timeout (SO_TIMEOUT) 
  // in milliseconds which is the timeout for waiting for data.
  int timeoutSocket = 10000;
  HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

  DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
  try {
   HttpResponse response = httpClient.execute(httpGet);
  } catch (ClientProtocolException e) {
   System.out.println(e.getLocalizedMessage());
   e.printStackTrace();
  } catch (IOException e) {
   System.out.println(e.getLocalizedMessage());
   e.printStackTrace();
  }

The Android manifest declares the service like this

<service android:name=".Notifications.PushService" 
       android:label="Application Notifications Service">

    </service>

I do have uses internet in the permissions. Any help would be appreciated.

Thanks!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜