I have the following code:

HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(SERVERURI + "site.php"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("login", "Login")); nameValuePairs.add(new BasicNameValuePair("password", "Password")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); 

Everything works, but I really don’t like the fact that half of it is crossed out (deprecated). How can I fix this? (update)

  • Once the great sage said: "It works - do not touch." (c) - iFr0z
  • @ iFr0z, you with such an approach to business, you will not leave far. - Edgar Asatryan
  • @EdgarAsatryan dOleko I will not leave, but it may not be enough) PS joke-a-minute - iFr0z
  • one
    @ iFr0z, thanks, did not notice. - Edgar Asatryan

1 answer 1

The DefaultHttpClient class DefaultHttpClient marked obsolete. From the official Apache documentation

Deprecated. (4.3) use HttpClientBuilder see also CloseableHttpClient.

Here you can see how to "update".


The official Android documentation recommends using HttpURLConnection .

If you decide to migrate to HttpsURLConnection , then you will most likely need AsyncTask , and using this bundle can result in very large and redundant code.


You can use higher-level libraries, for example

Android Asynchronous Http Client

 dependencies { compile 'com.loopj.android:android-async-http:1.4.8' } 

UPD 09/16/2016

More elegant HTTP client Retrofit

 dependencies { compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.0.2' compile 'com.google.code.gson:gson:2.6.1' }