Skip to content

Instantly share code, notes, and snippets.

@ccjeng
Last active October 30, 2021 19:40
Show Gist options
  • Save ccjeng/9a3cd64577e5f86d8357 to your computer and use it in GitHub Desktop.
Save ccjeng/9a3cd64577e5f86d8357 to your computer and use it in GitHub Desktop.
Okhttp callback sample
OkHttpClient client = new OkHttpClient();
String url = "http://www.google.com";
Request request = new Request.Builder()
.url(url)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
if (response.isSuccessful()) {
Log.d(TAG, response.body().toString());
// Run view-related code back on the main thread
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//Do someting
}
});
} else {
//Response Failed
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment