Last active
February 1, 2016 03:17
-
-
Save ruan65/f0ec9e3e61ef8a18a12b to your computer and use it in GitHub Desktop.
Howto okHttp3
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
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