我在同步库中遇到了一些奇怪的问题。它在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连接并尝试在此服务器URL上ping并在Android 9手机上获得响应,但是通过上述代码,我无法制作一个仅在Android 9手机上致电。有人可以知道吗?
在新的Android版本上禁用清晰的文本流量
从Android 9(API级别28(开始,默认情况下禁用了ClearText支持。
更改您的AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
资源:Android 8:ClearText HTTP流量不允许
这与Android 9中的SSL执行有关。我创建了一个适当的SSLFACTORY,并使用Trustmanager使用此帖子来解决此问题: