Wednesday, December 28, 2016

Replace Deprecated HTTP client/mime by Okhttp/Retrofit library while making API calls in Android

Hope everyone know recently Google has did great change on networking API, and most effective one on developer side is org.apach    Apache

So now it is every Android Developer job to kick out their own methods/classes/interfaces which they created to
make network(using Apache http client/mime.) call during application execution

The alternate solution which every application uses is OkHttp or Retrofit

Hope your existing code base having network calls like :


Network call using http client/mime to POST params and get response

POST Method


 String response = null;
        try {
            loginapi_call = new URI(context.getString(R.string.api_url)
                    + operation);
            // Log.e(TAG, loginapi_call.toString());
            HttpClient client = HttpClientFactory.getThreadSafeClient(context);
            HttpPost request = new HttpPost();
            request.setURI(loginapi_call);
            request.addHeader("Content-Type", "application/x-www-form-urlencoded");

            request.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 20000);
            HttpConnectionParams.setSoTimeout(httpParameters, 30000);
            HttpConnectionParams.setTcpNoDelay(httpParameters, true);
            request.setParams(httpParameters);

            long i = System.currentTimeMillis();// Log to know the time diff
            response = client.execute(request, responseHandler);

            // response = client.execute(request, httpContext);
            long j = System.currentTimeMillis();// Log to know the time diff
            // Log.i("Time Taken", j-i+" ms");

            // //Log(TAG,operation + " successful, response length:"+
            // response.length());
        } catch (Exception e) {
            e.printStackTrace();
            // Log.i("Error !!!", "In Request");
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                }
        }

POST METHOD using OkHttp
 String resp_str = null;

        Request request = new Request.Builder()
                .url(context.getString(R.string.api_url) + OPERATION)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .method("POST", RequestBody.create(null, new byte[0]))
                .post(requestBody)
                .build();

        try {

            OkHttpClient okHttpClient = new OkHttpClient().newBuilder().connectTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).build();

            Response response = okHttpClient.newCall(request).execute();

            resp_str = response.body().string();


          //  Log.e(TAG, response.body().string());

        } catch (IOException e) {
            e.printStackTrace();
        }
Reason to switch new netwoking libary click here

0 comments:

Post a Comment