本文共 3557 字,大约阅读时间需要 11 分钟。
com.squareup.okhttp3:okhttp:3.9.0 for OkHttp dependenciesexecute() method to send synchronus requestsNotes:
Example Code:
OkHttpClient mClient = new OkHttpClient.Builder().build();Request request = new Request.Builder().url("http://www.baidu.com").get().build();Call call = mClient.newCall(request);try { Response response = call.execute(); LogUtils.json(response.body().string());} catch (IOException e) { e.printStackTrace();} enqueue() method for asycnous requestsNotes:
onResponse() and onFailure() callbacks are executed in the worker threadExample Code:
OkHttpClient mClient = new OkHttpClient.Builder().build();Request request = new Request.Builder().url("http://www.baidu.com").get().build();Call call = mClient.newCall(request);call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { LogUtils.json(response.body().string()); runOnUiThread(new Runnable() { @Override public void run() { tvShow.setText("eeeeee"); } }); }}); execute() vs enqueue())OkHttpClient mClient = new OkHttpClient.Builder().build();
Explanation:
Request request = new Request.Builder().url("http://www.baidu.com").get().build(); Explanation:
Call call = mClient.newCall(request);
Explanation:
Call call = mClient.newCall(request);try { Response response = call.execute(); LogUtils.json(response.body().string());} catch (IOException e) { e.printStackTrace();} Explanation:
execute() method sends synchronus requestrunningSyncCalls queueBy following this guide, developers can effectively utilize OkHttp's synchronus and asycnous features in their applications, ensuring efficient network communication.
转载地址:http://puttz.baihongyu.com/