Skip to content

Instantly share code, notes, and snippets.

@vedhavyas
Last active April 18, 2016 12:03
Show Gist options
  • Save vedhavyas/1061887c5867029dda7b23105eae3ecf to your computer and use it in GitHub Desktop.
Save vedhavyas/1061887c5867029dda7b23105eae3ecf to your computer and use it in GitHub Desktop.
POST Request example in Android
//Add library by adding the following line in dependencies section in gradle file
compile 'com.squareup.okhttp3:okhttp:3.2.0'
//Making a Post request
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("name", "Name Here")
.add("email", "email here")
.add("purpose", "purpose here")
.build();
HashMap<String, String> headers = new HashMap<>();
headers.put("Header key", "Header Value");
okhttp3.Request request = new okhttp3.Request.Builder()
.url("Url here")
.headers(Headers.of(headers))
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//Handle error here
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//Handle response here
}
});
//You can access the other recipes for this lib at - https://github.com/square/okhttp/wiki/Recipes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment