gpt4 book ai didi

android - Volley 中的 onPreExecute 方法

转载 作者:行者123 更新时间:2023-11-30 02:51:55 25 4
gpt4 key购买 nike

volley 中有没有像 AsyncTask 这样的方法?

我在我的应用程序中使用了 volley 库,我想在 volley 中做一些预执行。那么在 AsyncTask 中的 volley lib 广告中是否有类似以下方法的方法,因为我想使用 Base64 图像字符串创建 json,当我要使用图像创建 json 时,我的应用程序停止响应。

protected void onPreExecute() {
pDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Retrieving data ...", true);
}

protected Void doInBackground(Void... unused) {
items = parser.getItems();

for (Item it : items) {
publishProgress(it);
}
return(null);
}

protected void onProgressUpdate(Item... item) {
adapter.add(item[0]);
}

protected void onPostExecute(Void unused) {
pDialog.dismiss();
}

谢谢

最佳答案

您可以将 ProgressDialog 添加到您的布局,然后在响应时将其可见性更改为 View.GONE

布局示例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

...

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />

</RelativeLayout>

volley 库使用示例:

JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
// add your logic here
progressBar.setVisibility(View.GONE);
// set you progressBar variable using findViewById method
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub

}
});

// Access the RequestQueue through your singleton class.
VolleySingleton.getInstance(getActivity()).addToRequestQueue(jsObjRequest);

关于android - Volley 中的 onPreExecute 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23992981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com