我的 android 应用程序需要对 php 脚本执行 HTTP 发布。该帖子需要在服务中发生。出于某种原因,POST 在服务中运行时连接超时,但如果我将该方法复制到我的应用程序的 onCreate 事件并运行,它工作正常。我不知道会有什么不同。 这是我的方法(超时代码基于this answer by kuester2000)
String url = "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();
}
Android 清单声明这样的服务
<service android:name=".Notifications.PushService"
android:label="Application Notifications Service">
</service>
我确实有使用互联网的权限。 任何帮助将不胜感激。
谢谢!