-2

As HttpClient is deprecated now for API 22,how could I perform Posting JSON data in URL using HttpUrlConnection for the below code:

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

        nameValuePairs.add(new BasicNameValuePair("location", arg0[1]));            
HttpClient httpclient = new DefaultHttpClient();   
      HttpPost httppost = new HttpPost(arg0[0]);  
       HttpParams httpParameters = new BasicHttpParams();  
        httpclient = new DefaultHttpClient(httpParameters);
          try {       
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));          HttpResponse response;  
           response = httpclient.execute(httppost);    
         StatusLine statusLine = response.getStatusLine(); 
            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {              Log.e("Google", "Server Responded OK");     
         } else {        
          response.getEntity().getContent().close();   
              throw new IOException(statusLine.getReasonPhrase());
             }   
     } catch (Exception e) {  
           e.printStackTrace();    
     } 
Aakash
  • 5,181
  • 5
  • 20
  • 37
susane
  • 5
  • 1
  • 1
    possible duplicate of [Sending a JSON HTTP POST request from Android](http://stackoverflow.com/questions/13911993/sending-a-json-http-post-request-from-android) – Daniel Nugent Sep 14 '15 at 18:41
  • 1
    Hello Susan, welcome to stackoverflow. This is a very generic question, if you google it you will find many answer such as above link or this link - http://stackoverflow.com/a/13486223/609782 . Happy coding. – Darpan Sep 14 '15 at 18:49
  • @ Darpan NameValue pair is also deprecated! – susane Sep 14 '15 at 19:16

1 Answers1

0

You can use this HTTP & SPDY client. It makes your code readeble and stable. http://square.github.io/okhttp/

Cüneyt
  • 2,565
  • 25
  • 31