3

I am porting my code to android 6.0. Since apache classes have already been deprecated and removed in API 23, I cant find a exact match for my previous code to make a HTTPS connection. Following is my code

try {


        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schemeRegistry.register(new Scheme("https",new CustomSSLSocketFactory(), 443));// new CustomSSLSocketFactory().newSslSocketFactory(context.getResources()), 443));

        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 120000);
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParameters, schemeRegistry);
        httpClient = new DefaultHttpClient(cm, httpParameters);
        setHttpClient(httpClient);

        AbstractMediator.setServerTime(System.currentTimeMillis());
        httpResponse = httpClient.execute(request);

        serviceResponseObject.setResponseCode(httpResponse.getStatusLine().getStatusCode());
        serviceResponseObject.setMessage(httpResponse.getStatusLine().getReasonPhrase());
        serviceResponseObject.setAllHeader(httpResponse.getAllHeaders());


        Header[] header = httpResponse.getAllHeaders();

    }catch (UnknownHostException e){
        Utility.printStackTrace(e);
        serviceResponseObject.setResponseBody("");
        return responseWithErrorCode(NETWORK_ERROR_CODE101, serviceResponseObject);
    }  catch (IOException e) {
        Utility.printStackTrace(e);
        return responseWithErrorCode(NETWORK_ERROR_CODE100, serviceResponseObject);
    } catch (Exception e) {
        Utility.printStackTrace(e);
        return responseWithErrorCode(NETWORK_ERROR_CODE100, serviceResponseObject);
    }

I have read this link Deprecated: org.apache.http.conn.scheme.scheme is deprecated. It do mention about scheme class new constructor, but not about rest of the methods. My app min API level is 14 and would be glad if any substitute method do respect it.

I have read this 'org.apache.http.HttpEntity' is deprecated. How to solve this error in android studio? which talks about using apache legacy and the other solution regarding Marek Sebera's new Apache HttpClient package for Android, but that does not serve my purpose.

I cant use libraries like Volley, Retrofit, OKHttp as i have web services in Soap XML format

I want to migrate away from apache completely. My concern is to know what all methods in HttpsUrlConnection class can be used for replacement. I would appreciate if i get a code regarding this which do exactly what i am doing above but with new API methods. I came to know about classes like Registry, PoolingHttpClientConnectionManager but i cant understand how to use them to attain above results

Community
  • 1
  • 1
Dhiraj Sharma
  • 105
  • 1
  • 8
  • use volley or retrofit or OkHttp the replacement of default http classes. –  Jun 01 '16 at 07:37
  • @satnamsingh thanks for suggestion, But i would prefer HTTPUrlConnection rather as all my web services are in SOAP xml – Dhiraj Sharma Jun 01 '16 at 07:40
  • read in http://stackoverflow.com/questions/30556605/org-apache-http-httpentity-is-deprecated-how-to-solve-this-error-in-android-s , it will be possible duplicate. – Htoo Aung Hlaing Jun 01 '16 at 07:54
  • @HtooAungHlaing I know about apache legacy, but it seems to be a temporary solution for now as given in this link https://github.com/androidquery/androidquery/issues/116. Also using apache legacy causes my application to crash on several occasions – Dhiraj Sharma Jun 01 '16 at 08:00
  • 1
    Marek Sebera's new Apache HttpClient package for Android http://stackoverflow.com/a/32805048/2772552 – Htoo Aung Hlaing Jun 01 '16 at 08:46
  • @HtooAungHlaing Oh i appreciate that but thats not what i want. I want to migrate away from apache completely. My concern is to know what all methods in HttpsUrlConnection class can be used for replacement. I would appreciate if i get a code regarding this which do exactly what i am doing above but with new API methods. I came to know about classes like Registry, PoolingHttpClientConnectionManager but i cant understand how to use them to attain above results – Dhiraj Sharma Jun 01 '16 at 09:42

1 Answers1

0

Apache HTTP client deprecation

According to Google, with Android 6.0, they removed support for the Apache HTTP client. Beginning with Android 9, that library is removed from the bootclasspath and is not available to apps by default.

So if you still want to make it available to your application Google has given the solution for that also.

Use the below tag inside application of your AndroidManifest.xml file

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

Refer the Android documentation for more details about the migration process. https://developer.android.com/about/versions/pie/android-9.0-changes-28#apache-p

Googlian
  • 6,077
  • 3
  • 38
  • 44