Skip to content

Instantly share code, notes, and snippets.

@Asutosh11
Last active November 30, 2016 11:28
Show Gist options
  • Save Asutosh11/6ce7ee2e04c3e7d2a3195cf2b14c6d96 to your computer and use it in GitHub Desktop.
Save Asutosh11/6ce7ee2e04c3e7d2a3195cf2b14c6d96 to your computer and use it in GitHub Desktop.
OkHttp Class for making REST API calls
/*
OkHttp class for making REST calls.
Here the method is POST, you can change it to GET in 'request' below
*/
public class ResponseOKhttp
{
OkHttpClient client = new OkHttpClient();
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
public String run(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
/*
Usage Example
-------------------------------------------------------------------------
// Just make an Object like this to use the class
ResponseOKhttp example = new ResponseOKhttp();
String response = null;
try {
response = example.run(url, json.toString());
Log.d("responsegot",response);
  }
catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment