AsyncHttpClient无法连接到Android 9上的发布URL

时间:2019-07-09 14:46:18

标签: android asynchttpclient android-async-http

我在SyncHttpClient库中遇到了一些奇怪的问题。它在android 9以下的设备上运行良好,但是我的代码无法对URL进行其余调用。 这是我的示例代码:

import com.loopj.android.http.*;

    AsyncHttpClient client = new SyncHttpClient();
                try {
                    KeyStore trustStore = MySSLSocketFactory.getKeystore();
                    MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
                    sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                    client.setSSLSocketFactory(sf);
                    client.setTimeout(600000);
                    client.setConnectTimeout(10000);
                } catch (Exception e) {
                    Log.e(TAG, "error while using truststore for apk instllation..");
                }

                // method which making a rest call
                 private void checkId(final String myUrl, final String name, final Integer version) {
        RequestParams requestParams = new RequestParams("name", name, "version", version);

        client.post(myUrl, requestParams, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try {
                    if (response == null) {
                        Log.i(TAG, "check_version response is null nothing to download.");
                        return;
                    }
                      //success

                } catch (JSONException e) {
                    Log.e(TAG, "Error while parsing json", e);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String res, Throwable throwable) {
                super.onFailure(statusCode, headers, res, throwable);
                Log.e(TAG, "Failed to check id " + myUrl + ", status " + statusCode + " error: "+throwable.getMessage());
            }
        });
    }

这是一个例外:

07-09 15:53:46.760 990-1008/com.mycompany W/System.err:     java.net.SocketTimeoutException: failed to connect to myserver.com/192.168.4.24 (port 8182) from /192.168.3.246 (port 56576) after 10000ms
07-09 15:53:46.762 990-1008/com.mycompany W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:185)
07-09 15:53:46.764 990-1008/com.mycompany W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:129)
07-09 15:53:46.766 990-1008/com.mycompany W/System.err:     at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:137)
07-09 15:53:46.767 990-1008/com.mycompany W/System.err:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
07-09 15:53:46.768 990-1008/com.mycompany W/System.err:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
07-09 15:53:46.769 990-1008/com.mycompany W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
07-09 15:53:46.770 990-1008/com.mycompany W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
07-09 15:53:46.771 990-1008/com.mycompany W/System.err:     at java.net.Socket.connect(Socket.java:621)
07-09 15:53:46.772 990-1008/com.mycompany W/System.err:     at com.android.org.conscrypt.ConscryptSocketBase.connect(ConscryptSocketBase.java:152)
07-09 15:53:46.773 990-1008/com.mycompany W/System.err:     at cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:524)
07-09 15:53:46.774 990-1008/com.mycompany W/System.err:     at cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:403)
07-09 15:53:46.775 990-1008/com.mycompany W/System.err:     at cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:472)
07-09 15:53:46.776 990-1008/com.mycompany W/System.err:     at cz.msebera.android.httpclient.conn.scheme.SchemeSocketFactoryAdaptor.connectSocket(SchemeSocketFactoryAdaptor.java:65)
07-09 15:53:46.776 990-1008/com.mycompany W/System.err:     at cz.msebera.android.httpclient.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177)
07-09 15:53:46.777 990-1008/com.mycompany W/System.err:     at cz.msebera.android.httpclient.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:145)
07-09 15:53:46.777 990-1008/com.mycompany W/System.err:     at cz.msebera.android.httpclient.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:131)
07-09 15:53:46.778 990-1008/com.mycompany W/System.err:     at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
07-09 15:53:46.779 990-1008/com.mycompany W/System.err:     at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
07-09 15:53:46.780 990-1008/com.mycompany W/System.err:     at cz.msebera.android.httpclient.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:860)
07-09 15:53:46.780 990-1008/com.mycompany W/System.err:     at cz.msebera.android.httpclient.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
07-09 15:53:46.781 990-1008/com.mycompany W/System.err:     at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:146)
07-09 15:53:46.781 990-1008/com.mycompany W/System.err:     at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:177)
07-09 15:53:46.783 990-1008/com.mycompany W/System.err:     at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:106)
07-09 15:53:46.783 990-1008/com.mycompany W/System.err:     at com.loopj.android.http.SyncHttpClient.sendRequest(SyncHttpClient.java:95)
07-09 15:53:46.784 990-1008/com.mycompany W/System.err:     at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:1169)
07-09 15:53:46.785 990-1008/com.mycompany W/System.err:     at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:1152)
07-09 15:53:46.785 990-1008/com.mycompany W/System.err:     at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:1139)
07-09 15:53:46.786 990-1008/com.mycompany W/System.err:     at mycompany.proj.InstallerService.checkId(InstallerService.java:87)
07-09 15:53:46.787 990-1008/com.mycompany W/System.err:     at mycompany.proj.InstallerService.onHandleIntent(InstallerService.java:76)
07-09 15:53:46.788 990-1008/com.mycompany W/System.err:     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:76)
07-09 15:53:46.789 990-1008/com.mycompany W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
07-09 15:53:46.789 990-1008/com.mycompany W/System.err:     at android.os.Looper.loop(Looper.java:193)
07-09 15:53:46.790 990-1008/com.mycompany W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:65)

我已经仔细检查了WIFI连接和其他电话设置,而且一切似乎都很好,我也连接了adb shell,并尝试ping此服务器URL并在android 9手机上获得响应,但通过上述代码我无法做出仅在android 9手机上致电。有人可以提供任何线索吗?

2 个答案:

答案 0 :(得分:0)

在新的Android版本上禁用了纯文本流量

  

从Android 9(API级别28)开始,默认情况下禁用明文支持。

更改您的AndroidManifest

Table1.ID

资源:Android 8: Cleartext HTTP traffic not permitted

答案 1 :(得分:0)

这与android 9中的SSL实施有关。我创建了一个正确的SSLFactory,并使用trustmanager通过以下post解决此问题: