Last active
April 18, 2016 12:03
-
-
Save vedhavyas/1061887c5867029dda7b23105eae3ecf to your computer and use it in GitHub Desktop.
POST Request example in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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