我正在开展一个项目,在该项目中,用户的位置将被持续跟踪。我正在使用保险丝位置api。我注意到我定期获取位置更新,但问题是将此位置发送到服务器。当电话空闲时,使用下面的代码发送位置时,它将显示“java.net.UnknownHostException:无法解析主机”myurl“:没有与主机名关联的地址”
我正在使用此代码将位置发送到服务器:
try {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient(params);
HttpParams httpParameters = httpclient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, TIMEOUT);
HttpConnectionParams.setTcpNoDelay(httpParameters, true);
if (activity == null)
callAPI(uri, httpclient);
else if (commonMethods.knowInternetOn(activity)) {
callAPI(uri, httpclient);
} else {
commonMethods.showInternetAlert(activity);
}
} catch (Exception e) {
e.printStackTrace();
}
private void callAPI(String[] uri, HttpClient httpclient) {
try {
HttpResponse response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} else {
response.getEntity().getContent().close();
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// Toast.makeText(activity, "Server Error!",
// Toast.LENGTH_SHORT).show();
responseString = null;
}
});
}
throw new IOException(statusLine.getReasonPhrase());
}
} catch (UnknownHostException ue) {
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
commonMethods.displayToastAboveButton(activity, "Server Not Found!");
responseString = null;
}
});
}
} catch (SocketTimeoutException se) {
se.printStackTrace();
responseString = null;
} catch (ConnectTimeoutException se) {
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
commonMethods.displayToastAboveButton(activity, "Internet Problem!");
responseString = null;
}
});
}
} catch (Exception e) {
e.printStackTrace();
responseString = null;
}
}
我的问题是: 即使在电话闲置期间,如何在我的应用程序中获取INTERNET ACCESS?
注意:当我按下电源按钮,电话再次唤醒时,一切正常,即没有任何未知的主机异常。