2

我的应用程序中有一个发生此异常的网络进程,但我不知道如何解决此问题。我已经在清单中应用了某些权限,这是我为整个应用程序应用的权限:

<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
    }
}
4

2 回答 2

5

我的设备也面临同样的问题。带有 json 响应的 URI 在浏览器中工作正常,但在设备中它会引发异常。我只是重新启动了我的设备,然后它就可以在移动端工作了。您尝试重新启动设备。

于 2013-12-15T10:31:04.970 回答
0

请遵循这些解决方案,这些解决方案可能会解决您的问题。

1>在那里检查您的清单文件互联网权限。

2> 通过休息客户端使用浏览器检查您的网址并传递适当的请求。

3> 在手机中打开网址,例如:- http://您的IP 地址/端口,仅用于检查您是否有权在手机中打开此网址。

于 2015-02-10T09:42:42.373 回答