我的android应用程序需要对PHP脚本执行HTTP发布。帖子需要在服务中发生。出于某种原因,POST在服务中运行时会获得连接超时,但如果我将方法复制到我的应用程序的onCreate事件并运行,它可以正常工作。我不知道会有什么不同。 这是我的方法
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>
我确实在权限中使用了互联网。 任何帮助将不胜感激。
谢谢!