我的应用程序中有一个网络进程发生此异常,我不知道如何解决这个问题。我已在我的清单中应用了某些权限,这是我为整个应用程序申请的权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
我正在使用的URI来自我们的服务器的全局IP地址。它在浏览器中工作正常但在应用程序中出现此异常。这是堆栈跟踪:
08-12 06:15:36.234: W/System.err(862): org.apache.http.conn.HttpHostConnectException: Connection to http://xxx.xx.xxx.xxx:8080 refused
08-12 06:15:36.324: W/System.err(862): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
08-12 06:15:36.494: W/System.err(862): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-12 06:15:36.594: W/System.err(862): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-12 06:15:36.785: W/System.err(862): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-12 06:15:36.904: W/System.err(862): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-12 06:15:37.014: W/System.err(862): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-12 06:15:37.164: W/System.err(862): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-12 06:15:37.194: W/System.err(862): at com.android.app.util.Utility.getRequest(Utility.java:42)
08-12 06:15:37.284: W/System.err(862): at com.android.app.LoginActivity$LoginTask.doInBackground(LoginActivity.java:440)
08-12 06:15:37.404: W/System.err(862): at com.android.app.LoginActivity$LoginTask.doInBackground(LoginActivity.java:1)
08-12 06:15:37.534: W/System.err(862): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-12 06:15:37.744: W/System.err(862): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-12 06:15:37.804: W/System.err(862): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-12 06:15:37.944: W/System.err(862): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-12 06:15:38.044: W/System.err(862): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-12 06:15:38.164: W/System.err(862): at java.lang.Thread.run(Thread.java:856)
08-12 06:15:38.364: W/System.err(862): Caused by: java.net.ConnectException: failed to connect to /xxx.xx.xxx.xxx (port 8080): connect failed: ETIMEDOUT (Connection timed out)
08-12 06:15:38.394: W/System.err(862): at libcore.io.IoBridge.connect(IoBridge.java:114)
08-12 06:15:38.664: W/System.err(862): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
08-12 06:15:38.845: W/System.err(862): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
08-12 06:15:38.894: W/System.err(862): at java.net.Socket.connect(Socket.java:842)
08-12 06:15:39.085: W/System.err(862): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
08-12 06:15:39.154: W/System.err(862): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
08-12 06:15:39.184: W/System.err(862): ... 15 more
08-12 06:15:39.244: W/System.err(862): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
08-12 06:15:39.464: W/System.err(862): at libcore.io.Posix.connect(Native Method)
08-12 06:15:39.774: W/System.err(862): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
08-12 06:15:39.804: W/System.err(862): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
08-12 06:15:39.894: W/System.err(862): at libcore.io.IoBridge.connect(IoBridge.java:112)
08-12 06:15:39.984: W/System.err(862): ... 20 more
任何帮助将不胜感激。谢谢。
修改 好的,所以这是我的实用工具:
public static String getRequest(String path){
String url = http://xxx.xx.xxx.xxx:8080/SampleApi/sample-apis; // Global URL of the API
String username = "sampleuname";
String password = "samplepass";
CredentialsProvider credProvider = new BasicCredentialsProvider();
credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials(username, password));
try{
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.setCredentialsProvider(credProvider);
HttpGet httpGet = new HttpGet(url + path);
HttpResponse httpResponse = httpClient.execute(httpGet); // Okay, so the stack trace is pointing in this line
HttpEntity httpEntity = httpResponse.getEntity();
if(httpEntity != null){
inStream = httpEntity.getContent();
String jsonString = convertStreamToString(inStream);
return jsonString;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "Request NULL";
}
和我的召唤Asynctask:
public class LoginTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... path) {
Log.d(Constant.TAG_LOGIN, path[0]);
String apiRequestReturn = Utility.getRequest(path[0]); // So here's the other line pointed by the stack trace where I called Utility
if (apiRequestReturn.equals("")) {
Log.d(Constant.TAG_LOGIN, "WebService request is null");
return null;
} else {
Log.d(Constant.TAG_LOGIN, "WebService request has data");
return apiRequestReturn;
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(null);
//JSON PARSING
}
}
答案 0 :(得分:5)
我也遇到了与我的设备相同的问题。带有json响应的URI在浏览器中工作正常,但在设备中它会抛出异常。我只是重新启动我的设备然后它在移动端工作。您尝试重新启动设备。
答案 1 :(得分:0)
请按照这些解决方案解决您的问题。
1&GT;检查你的清单文件是否有互联网许可。
2 - ;通过rest客户端使用浏览器检查您的URL并传递相应的请求。
3&GT;在移动设备中打开网址,例如: - http://your IP地址/端口,仅用于检查您是否有权在移动设备中打开此网址。