when I try to create new HttpClient, it appears like this ̶H̶t̶t̶p̶C̶l̶i̶e̶n̶t̶
Asked
Active
Viewed 237 times
-4
-
its just to acknowledge you that HttpClient is deprecated . still you can use it , if you want to – Tejas Pandya Dec 30 '17 at 12:28
1 Answers
2
when I try to create new HttpClient, it appears like this H̶t̶t̶p̶C̶l̶i̶e̶n̶t̶
The method HttpClient
is deprecated.
you can use HttpURLConnection
here is the simple demo code how to use HttpURLConnection
with AsyncTask
public static class GetAsyncTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
StringBuffer result = new StringBuffer("");
try {
URL url = new URL("https://stackoverflow.com/");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// here you can set request method like GET and POST
httpURLConnection.setConnectTimeout(20000);// here you can set connection time out
httpURLConnection.setReadTimeout(20000);// here you can set Read Time out
httpURLConnection.connect();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
}
}
than use like this
GetAsyncTask getAsyncTask = new GetAsyncTask();
try {
String str=getAsyncTask.execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}

Goku
- 9,102
- 8
- 50
- 81
-
-
-
-
-
-
@HossameMakhlof no you can use both GET and POST method as per your requirement check in my ans i have mention it – Goku Dec 30 '17 at 12:34
-
1
-