Created
June 23, 2017 02:22
-
-
Save mako34/2de02f56948bd7f8d09719d644541999 to your computer and use it in GitHub Desktop.
android OKHTTP3 async GET
This file contains 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
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