Junior — Middle
What main function does the OkHttp library perform in applications?
sobes.tech AI
Answer from AI
OkHttp is a library for Android and Java that handles the execution of HTTP requests. Its main function is to simplify and optimize network interactions, providing a convenient and efficient way to send requests and receive responses from servers. It supports features such as connection pooling, caching, retries, and asynchronous calls.
Example of a simple GET request with OkHttp:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://api.example.com/data")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String responseData = response.body().string();
System.out.println(responseData);
}
}
});