Skip to content

Instantly share code, notes, and snippets.

@xxhdpi
Created March 27, 2017 00:03
Show Gist options
  • Select an option

  • Save xxhdpi/9312cc6ba4f6ccaefe636102504f9fb9 to your computer and use it in GitHub Desktop.

Select an option

Save xxhdpi/9312cc6ba4f6ccaefe636102504f9fb9 to your computer and use it in GitHub Desktop.
/**
* ambil data web dengan OkHttp
*/
private void ambilDataWeb(String url) {
// create request
Request request = new Request.Builder().url(url).build();
// create async request call
httpClient.newCall(request).enqueue(new Callback() {
@Override public void onFailure(Call call, IOException e) {
Log.e(TAG, "onFailure: " + e.getLocalizedMessage());
}
@Override public void onResponse(Call call, final Response response) throws IOException {
final String result = response.body().string();
Log.d(TAG, "onResponse: " + result);
// set response hasil to textview
if (response.isSuccessful()) {
// pass result to UIThread
runOnUiThread(new Runnable() {
@Override public void run() {
tvHasil.setText(result);
}
});
} else {
throw new IOException("Unexpected code " + response);
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment