Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import org.json.JSONException;
  2. import org.json.JSONObject;
  3.  
  4. import java.io.IOException;
  5.  
  6. import okhttp3.Call;
  7. import okhttp3.Callback;
  8. import okhttp3.MediaType;
  9. import okhttp3.OkHttpClient;
  10. import okhttp3.Request;
  11. import okhttp3.RequestBody;
  12. import okhttp3.Response;
  13. import okhttp3.MediaType;
  14. import okhttp3.OkHttpClient;
  15.  
  16. public class Post {
  17. private static final MediaType JSON
  18. = MediaType.parse("application/json; charset=utf-8");
  19.  
  20. OkHttpClient client = new OkHttpClient();
  21.  
  22. public String post(String url, String json) throws IOException {
  23. RequestBody body = RequestBody.create(JSON, json);
  24. Request request = new Request.Builder()
  25. .url(url)
  26. .post(body)
  27. .build();
  28. Response response =client.newCall(request).enqueue(new Callback() {
  29. @Override
  30. public void onFailure(Call call, IOException e) {
  31.  
  32. }
  33.  
  34. @Override
  35. public void onResponse(Call call, final Response response) throws IOException {
  36. try {
  37. String responseData = response.body().string();
  38. JSONObject json = new JSONObject(responseData);
  39. final String owner = json.getString("name");
  40. } catch (JSONException e) {
  41.  
  42. }
  43. }
  44. });
  45.  
  46. return response.body().string();
  47.  
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement