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
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("http://myurl.com");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("key1", valu1)
.appendQueryParameter("key2", value2);
String query = builder.build().getEncodedQuery();
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
connection.connect();
source to share
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. http://square.github.io/okhttp/
I think this link will help you http://developer.android.com/reference/org/apache/http/impl/client/DefaultHttpClient.html
source to share
from this link you can download the latest jar for httpclient4.5. and use
HttpClient httpClient = HttpClientBuilder.create().build();
source to share
HttpClient
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.
source to share
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("http://www.google.com");
HttpResponse response = client.execute(request);
source to share