Skip to content

Instantly share code, notes, and snippets.

@ruan65
Last active February 1, 2016 03:17
Show Gist options
  • Save ruan65/f0ec9e3e61ef8a18a12b to your computer and use it in GitHub Desktop.
Save ruan65/f0ec9e3e61ef8a18a12b to your computer and use it in GitHub Desktop.
Howto okHttp3
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String postBody = "{ \"name\" : \"Android\", \"address\" : \"127.0.0.5\" }";
Request request = new Request.Builder()
.url("http://82.196.0.67:8080/register/robot")
.addHeader("Content-Type", "application/json")
.post(RequestBody.create(JSON, postBody))
.build();
client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Request request, IOException e) {
Timber.i(e.getMessage());
}
@Override
public void onResponse(okhttp3.Response response) throws IOException {
Timber.i(response.message());
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment