do android volley have callback do something like

2019-07-29 03:47发布

I have to do something after the request finish. like dismiss a loading dialog

I have to add dialog.dismiss(); in every request for both onResponse() and onErrorResponse().

Do volley have global callback? I want to have global callback in same fragment/activity to handle error/successful message

    JsonRequest jsonRequest = new JsonRequest
            (url, params, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    dialog.dismiss();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    dialog.dismiss();
                }
            }) {
    };

1条回答
Explosion°爆炸
2楼-- · 2019-07-29 04:39

Yes you can do this by creating custom volley request. See the code below :

import java.io.UnsupportedEncodingException;
import java.util.Map;    
import org.json.JSONException;
import org.json.JSONObject;    
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import android.app.AlertDialog;

public class CustomRequest extends Request<JSONObject> {
    private Listener<JSONObject> listener;
    private Map<String, String> params;
    private AlertDialog dialog;

 //You can replace AlertDialog by the Type of dialog you use
    public CustomRequest(String url, Map<String, String> params,
            Listener<JSONObject> reponseListener, ErrorListener          errorListener,AlertDialog dialog)
          {
        super(Method.GET, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
        this.dialog =dialog;
    }
    public CustomRequest(int method, String url, Map<String, String> params,
            Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }
    protected Map<String, String> getParams()
            throws com.android.volley.AuthFailureError {
        return params;
    }; 

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            //Here you can call dialog.dismiss();
            dialog.dismiss();
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }
    @Override
    protected void deliverResponse(JSONObject response) {
        // TODO Auto-generated method stub
        listener.onResponse(response);
    }
}
}

Let me know if this works. :)

查看更多
登录 后发表回答