Android 6.0 HTTPClient issue with LG G3 phone

后端 未结 2 2033
闹比i
闹比i 2021-02-06 19:16

Hi I am using DefaultHTTPClient class to create http request.

The currrent code execute() method works on all phones including Nexus and Samsung with Android 6.0.

<
2条回答
  •  别那么骄傲
    2021-02-06 19:57

    Give something like this a go, i had similar problem on MotoX running 6.0.1 but this seem to work for me.

     protected static final int HTTP_JSON_TIMEOUT_CONNECTION = 20000;
     protected static final int HTTP_JSON_TIMEOUT_SOCKET = 20000;
    
     CloseableHttpResponse response = null;
       try {
            UserPrefs.clearCacheFolder(mContext.getCacheDir());
            CloseableHttpClient httpClient = getApacheHttpClient();
            HttpPost httpPost = getHttpPost( invocation);
    
           response = httpClient.execute(httpPost);
        }catch (Exception e){
            e.printStackTrace();
        }
    
    
        protected CloseableHttpClient getApacheHttpClient(){
        try {
            // Socket config
            SocketConfig socketConfig = SocketConfig.custom()
                    .setSoTimeout(HTTP_JSON_TIMEOUT_SOCKET)
                    .build();
            // Auth
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(URL, PORT, null, "Digest"),
                    new UsernamePasswordCredentials(USERNAME,DIGEST_PASSWORD));
            // Build HttpClient
            return HttpClients.custom()
                    .setDefaultSocketConfig(socketConfig)
                    .setDefaultCredentialsProvider(credentialsProvider)
                    .build();
        }catch (Exception e) {
            Log.i("ApacheHttpClientHelper", "ERROR >> "+e.getMessage());
            return null;
        }
    }
    

    Also stick this into the gradle under dependencies

      compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
    

提交回复
热议问题