做android volley有回调做最后的事情吗?

时间:2016-03-02 02:32:28

标签: java android android-volley

请求完成后我必须做点什么。喜欢解除加载对话框

我必须在onResponse()和onErrorResponse()的每个请求中添加dialog.dismiss();

凌空有全球回调吗?我希望在相同的片段/活动中进行全局回调以处理错误/成功消息

    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 个答案:

答案 0 :(得分:0)

是的,您可以通过创建自定义排球请求来执行此操作。 请参阅以下代码:

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);
    }
}
}

如果有效,请告诉我。 :)