0

I want to use HTTP class get and post methods to fetch and upload data on server. I have upgraded my SDK and working on android API 22 but as org.apache.http class has been deprecated in API 22. Below are the code used before API 22.

InputStream in;
StringBuilder s = null;
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 1000*10); // 10 seconds Timeout
HttpResponse response;
String service_url = context.getResources().getString(R.string.url);

HttpGet get = new HttpGet(service_url);
response=client.execute(get);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
   in = response.getEntity().getContent();
   BufferedReader reader = new BufferedReader(new InputStreamReader(in));
   String sResponse;
   s = new StringBuilder();
   while ((sResponse = reader.readLine()) != null) {
       s = s.append(sResponse);
   }
   JSONObject jsonObject = new JSONObject(s.toString());
}
gabriel
  • 2,321
  • 4
  • 18
  • 23
  • you should have to use `HttpURLConnection` to overcome this problem. here are some helps.. http://stackoverflow.com/questions/29536233/deprecated-http-classes-android-lollipop-5-1 http://stackoverflow.com/questions/29058727/i-need-an-option-to-httpclient-in-android-to-send-data-to-php-as-it-is-deprecate – Ranjit May 22 '15 at 06:59
  • Thanks Ranjit,if we want to add parameters then how we will use parameter instead multi -part ? – Pradeep Kumar Verma May 22 '15 at 08:08

1 Answers1

1

Taken from here

URL url = new URL("http://yoururl.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

conn.setRequestMethod("POST");


List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("firstParam", paramValue1));
params.add(new BasicNameValuePair("secondParam", paramValue2));
params.add(new BasicNameValuePair("thirdParam", paramValue3));

Hope It should help :)

Community
  • 1
  • 1
Ranjit
  • 4,797
  • 3
  • 28
  • 62