Skip to content

Instantly share code, notes, and snippets.

@mako34
Created June 23, 2017 02:22
Show Gist options
  • Save mako34/2de02f56948bd7f8d09719d644541999 to your computer and use it in GitHub Desktop.
Save mako34/2de02f56948bd7f8d09719d644541999 to your computer and use it in GitHub Desktop.
android OKHTTP3 async GET
Log.d("mk", "chapas");
//call pelada
// final OkHttpClient client = new OkHttpClient();
//with stetho,
final OkHttpClient client = new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).build();
final Request request = new Request.Builder()
.url("http://www.mocky.io/v2/573336c90f0000902cead88d")
.build();
AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
try {
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
return null;
}
return response.body().string();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s != null) {
Log.d("mk", "response:"+s);
}
}
};
asyncTask.execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment