Skip to content

Instantly share code, notes, and snippets.

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

  • Save xxhdpi/3120fee7a208fac49e99451e16714ab5 to your computer and use it in GitHub Desktop.

Select an option

Save xxhdpi/3120fee7a208fac49e99451e16714ab5 to your computer and use it in GitHub Desktop.
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String URL_SERVICES = "https://httpbin.org/get";
private Button btnAmbilData;
private TextView tvHasil;
// declare http client
private OkHttpClient httpClient;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAmbilData = (Button) findViewById(R.id.btnAmbilData);
tvHasil = (TextView) findViewById(R.id.tvResult);
// inisiasi http client
httpClient = new OkHttpClient();
btnAmbilData.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
ambilDataWeb(URL_SERVICES);
}
});
}
/**
* 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