OkHttp的enqueue方法是否实现了AsyncTaskLoader?
Does OkHttp's enqueue method implement AsyncTaskLoader?
我对 android 编程还很陌生。我已经编写了一个代码来使用 OkHttp 库执行 API 查询。
我的问题是,一旦 OkHttp 的入队过程开始,用户立即旋转 phone,从而销毁并重新启动 activity,是否会导致第二次入队过程发生,从而导致代码并行执行 2 OkHttp Api 查询?
我知道如果我们将 httpUrlConnections 与 AsyncTask(而不是 OkHttp)一起使用,这是一个已知问题。我们可以使用 AsyncTaskLoader 绕过它,它的生命周期与我们的 MainActivity 是分开的。但我只是想知道 OkHttp 是否也考虑到了这一点。
谢谢!如果有帮助的话,我的代码示例如下所示。
private void performApiCall(String url) {
mLoadingPb.setVisibility(View.VISIBLE);
// is network available?
if (NetworkUtility.isNetworkAvailable(MainActivity.this)) {
// create new OkHttp client
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build(); // building the request
Call call = client.newCall(request);
// Asynchronous task
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
alertUserAboutError();
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingPb.setVisibility(View.INVISIBLE);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d(TAG, "OkHttp, onResponse received");
// perform data download
if (response.isSuccessful()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingPb.setVisibility(View.INVISIBLE);
}
});
String jsonData = response.body().string(); // this is the raw data in json format
Log.v(TAG, jsonData);
try {
if (mMovieData == null) {
saveMovieData(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
mCurrentPage++;
setRecyclerView();
Log.d(TAG, "OkHttpCall completed");
}
});
} else {
appendMovieData(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
mCurrentPage++;
updateRecyclerView();
Log.d(TAG, "OkHttpCall completed");
}
});
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, "Exception caught: ", e);
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingPb.setVisibility(View.INVISIBLE);
}
});
alertUserAboutError();
}
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingPb.setVisibility(View.INVISIBLE);
}
});
alertUserNetworkUnavailable();
}
}
My question is, once the OkHttp's enqueue process has started, and the user immediately rotates the phone, thereby destroying and restarting the activity, does that cause a second enqueue process to occur, thereby causing the code to perform 2 OkHttp Api query in parallel?
如果您的 activity 在创建时自动调用 enqueue()
,那么可以。
And we can bypass it by using an AsyncTaskLoader, which has a separate life cycle from our MainActivity.
请注意,架构组件对视图模型的支持和 LiveData
是 Google 解决加载程序试图解决的问题的当前方向。
我对 android 编程还很陌生。我已经编写了一个代码来使用 OkHttp 库执行 API 查询。
我的问题是,一旦 OkHttp 的入队过程开始,用户立即旋转 phone,从而销毁并重新启动 activity,是否会导致第二次入队过程发生,从而导致代码并行执行 2 OkHttp Api 查询?
我知道如果我们将 httpUrlConnections 与 AsyncTask(而不是 OkHttp)一起使用,这是一个已知问题。我们可以使用 AsyncTaskLoader 绕过它,它的生命周期与我们的 MainActivity 是分开的。但我只是想知道 OkHttp 是否也考虑到了这一点。
谢谢!如果有帮助的话,我的代码示例如下所示。
private void performApiCall(String url) {
mLoadingPb.setVisibility(View.VISIBLE);
// is network available?
if (NetworkUtility.isNetworkAvailable(MainActivity.this)) {
// create new OkHttp client
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build(); // building the request
Call call = client.newCall(request);
// Asynchronous task
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
alertUserAboutError();
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingPb.setVisibility(View.INVISIBLE);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d(TAG, "OkHttp, onResponse received");
// perform data download
if (response.isSuccessful()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingPb.setVisibility(View.INVISIBLE);
}
});
String jsonData = response.body().string(); // this is the raw data in json format
Log.v(TAG, jsonData);
try {
if (mMovieData == null) {
saveMovieData(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
mCurrentPage++;
setRecyclerView();
Log.d(TAG, "OkHttpCall completed");
}
});
} else {
appendMovieData(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
mCurrentPage++;
updateRecyclerView();
Log.d(TAG, "OkHttpCall completed");
}
});
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, "Exception caught: ", e);
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingPb.setVisibility(View.INVISIBLE);
}
});
alertUserAboutError();
}
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingPb.setVisibility(View.INVISIBLE);
}
});
alertUserNetworkUnavailable();
}
}
My question is, once the OkHttp's enqueue process has started, and the user immediately rotates the phone, thereby destroying and restarting the activity, does that cause a second enqueue process to occur, thereby causing the code to perform 2 OkHttp Api query in parallel?
如果您的 activity 在创建时自动调用 enqueue()
,那么可以。
And we can bypass it by using an AsyncTaskLoader, which has a separate life cycle from our MainActivity.
请注意,架构组件对视图模型的支持和 LiveData
是 Google 解决加载程序试图解决的问题的当前方向。