Http methods are forbidden in android studio

I am trying to make some HTTP requests with parameters. I am currently using

 private List<NameValuePair> mParams = new ArrayList<NameValuePair>();
 private DefaultHttpClient mHttpClient;
 private HttpPost mHttpPost;


to make a http request and it works fine. But the problem is that android studio is showing outdated warning for all these 3. I tried to use HttpClientBuilder.create().build();

but android studio cannot import libraries for HttpClientBuilder

I tried to download and add this jar dependency but still doesn't work. This is my code

        mHttpClient = new DefaultHttpClient();
        mHttpPost = new HttpPost(url);
        mParams.add(new BasicNameValuePair("key", value));
        mHttpPost.setEntity(new UrlEncodedFormEntity(mParams));


All of these lines show a deprecated warning, is there an alternative method for this?


source to share

5 answers

You can use HttpsURLConnection

instead DefaultHttpClient

. Also NameValuePair

deprecated, use Uri.Builder

also appendQueryParameter

to send query parameters.

Try the following piece of code:

URL url = new URL("");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
Uri.Builder builder = new Uri.Builder()
       .appendQueryParameter("key1", valu1)
       .appendQueryParameter("key2", value2);
String query =;
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
       new OutputStreamWriter(os, "UTF-8"));




The DefaultHttpClient class has been deprecated, you can use the HttpUrlConnection class instead. API HttpUrlConnection hassimple. The NameValuePair class may be deprecated in api 22. You can use ContentValues ​​(android.content.ContentValues) instead of NameValuePair.

ContentValues contentValues = new ContentValues();
        contentValues.put("Param_name1", Param_value1);
        contentValues.put("Param_name2", Param_value2);


I am using Okhttp client to receive and send a message.

I think this link will help you



from this link you can download the latest jar for httpclient4.5. and use

HttpClient httpClient = HttpClientBuilder.create().build();





deprecated since API level 22

. Prefer HttpURLConnection

for new code.

Android includes two HTTP clients: HttpURLConnection and Apache HTTP Client. Both support HTTPS, streaming downloads and uploads, configurable timeouts, IPv6

and connection pooling. Apache HTTP client has fewer bugs in Android 2.2 (Froyo) and earlier. HttpURLConnection

Best choice for Android 2.3 (Gingerbread) and later . Its simple API and small footprint make it very suitable for Android. Transparent compression and responsive caching reduces network usage, improves speed, and conserves battery. See Android Developers Blog for a comparison of the two HTTP clients.



The IDE asks for a warning in the new DefaultHttpClient , marking this class as deprecated [deprecated in Api 22] .

To solve this problem, use the HttpClientBuilder :

    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet("");
    HttpResponse response = client.execute(request);




All Articles