Last active
October 30, 2021 19:40
-
-
Save ccjeng/9a3cd64577e5f86d8357 to your computer and use it in GitHub Desktop.
Okhttp callback sample
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
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