Apache HttpClient
The Apache HttpClient library allows us to make HTTP requests.
- Add HttpClient Library to your project
1 2 3 |
dependencies { implementation 'org.apache.httpcomponents:httpclient:4.5.6' } |
For latest Version:- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
- Creating HTTP Client and HTTP Post
1 2 |
HttpClient httpClient = new DefaultHttpClient(); //Creating HTTP client HttpPost httpPost = new HttpPost("http://www.codebucket.in/api"); //Creating HTTP Post |
- Building Post Parameters:- The following code will create post parameters pair with key and value.
1 2 3 4 |
// Building post parameters, key and value pair List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2); nameValuePair.add(new BasicNameValuePair("username", "xyz")); nameValuePair.add(new BasicNameValuePair("password", "abc")); |
- URL Encoding POST data:-Before making HTTP request you need to encode the post data in order to convert all string data into valid URL format.
1 2 3 4 5 |
// Url Encoding the POST parameters try {httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair)); } catch (UnsupportedEncodingException e) {e.printStackTrace(); } |
- Finally you need to execute httpPost using the httpClient created before.
1 2 3 4 5 6 |
// Making HTTP Request try { HttpResponse response = httpClient.execute(httpPost); }catch (IOException e) { e.printStackTrace(); } |
Complete Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package in.codebucket.httpclientexample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost=new HttpPost("http://www.example.com/login"); List<NameValuePair> nameValuePair=new ArrayList<NameValuePair>(2); nameValuePair.add(new BasicNameValuePair("email", "user")); nameValuePair.add(new BasicNameValuePair("Hi","Hello")); try { httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try { HttpResponse response=httpClient.execute(httpPost); } catch (IOException e) { e.printStackTrace(); } } } |
Note:
- It is recommended to use OkHttp instead of HttpClient.
- For the sake of simplicity, we haven’t use any thread here. Don’t make any network call in UI thread.
Also, Refer
1) https://www.androidhive.info/2011/10/android-making-http-requests/
2) http://www.vogella.com/tutorials/ApacheHttpClient/article.html