【问题标题】:onPreExecute method in volleyvolley 中的 onPreExecute 方法
【发布时间】:2014-06-02 11:01:44
【问题描述】:

凌空有没有像AsyncTask这样的方法?

我在我的应用程序中使用了 volley lib,我想在 volley 中进行一些预执行。那么在 volley lib ad 中是否有任何方法,例如 AsyncTask 中的以下方法,因为我想使用 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();
    }

谢谢

【问题讨论】:

    标签: android android-asynctask progressdialog android-volley


    【解决方案1】:

    您可以在布局中添加 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);
    

    【讨论】:

      【解决方案2】:

      Volley 库没有像 onPreExecute 这样的方法。

      您可以在 Volley 中调用查询请求之前初始化变量。

      这是一个简单的示例,这段代码取自 Volley 库中包含的示例:

           //Here you'll initialize variables like showing dialog etc.
      
      
          RequestQueue queue = Volley.newRequestQueue(this);
                  String url = "https://ajax.googleapis.com/ajax/services/search/images?" +
                              "v=1.0&q=dog&rsz=8";
      
                  JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
      
                      @Override
                      public void onResponse(JSONObject response) {
                          // TODO Auto-generated method stub
                          Log.d("Response", response.toString());
                          //Here you'll dismiss dialog :)
                      }
                  }, new Response.ErrorListener() {
      
                      @Override
                      public void onErrorResponse(VolleyError error) {
                          // TODO Auto-generated method stub
      
                      }
                  });
      
                  queue.add(jsObjRequest);
      
              }
      

      【讨论】:

      • 是的,但是我在 Json 的 Base64 字符串中添加图像,所以当我在 UI 线程中创建 json 时应用程序停止响应。
      • 可以分享一下日志猫吗?
      • 不,这不是我要查找的日志。我想要你的应用停止响应的日志。
      • 我的应用不是强制关闭而是停止响应。
      • 如何解除?
      猜你喜欢
      • 2015-10-21
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 2015-01-09
      • 2014-11-12
      • 1970-01-01
      相关资源
      最近更新 更多