6

请问,Volley 会自动将我的 GET 参数添加到 URL 中吗?对我来说,它不起作用,而且在查看源代码时,我只是找不到任何 getParams 方法的调用。所以我应该自己构建 URL 吗?完全没问题,我只是觉得有getParams这样的方法,它可以为我做到这一点:)

更新:下面是我的代码..

public class BundleRequest extends com.android.volley.Request<Bundle>{

    private String token;
    private OnAuthTokenValidatorResponseListener mListener;
    private final Map<String, String> mParams =  new HashMap<String, String>();;


    public BundleRequest(int method, String url,  Response.ErrorListener listener) {
        super(method, url, listener);
    }

    public BundleRequest(int method, String url,OnAuthTokenValidatorResponseListener providedListener,  Response.ErrorListener listener, String token) {
        super(method, url, listener);
        this.token = token;
        mListener = providedListener;
        mParams.put(AuthenticatorConfig.TOKEN_VALIDATION_PARAMNAME, token);

    }

    @Override
    public Map<String, String> getParams() throws AuthFailureError {
        return mParams;
    }




    @Override
    protected Response<Bundle> parseNetworkResponse(NetworkResponse httpResponse) {
        switch (httpResponse.statusCode) {
            case AuthTokenValidator.TOKEN_VALID_RESPONSE_CODE:
                //token is ok
                JSONObject response;
                try {
                        response = new JSONObject(new String(httpResponse.data, HttpHeaderParser.parseCharset(httpResponse.headers)));
                        Bundle userDataResponse = new Bundle();
                        userDataResponse.putInt("responseCode", httpResponse.statusCode);
                        userDataResponse.putString("username", response.getString("user_id"));
                        userDataResponse.putString("email", response.getString("user_email"));
                        userDataResponse.putString("expiresIn", response.getString("expires_in"));
                        userDataResponse.putString("scope", response.getJSONArray("scope").getString(0));
                        userDataResponse.putString("token", token);
                    return Response.success(userDataResponse, HttpHeaderParser.parseCacheHeaders(httpResponse));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    return Response.error(new VolleyError("Unsupported encoding"));


                } catch (JSONException e) {
                    e.printStackTrace();
                    return Response.error(new VolleyError("Problem while parsing JSON"));
                }




            case AuthTokenValidator.TOKEN_INVALID_RESPONSE_CODE:
                //token is not valid
                mListener.onValidatorResponse(httpResponse.statusCode);
                try {
                    mListener.onValidatorResponse(parseOnErrorResponse(new String(httpResponse.data, HttpHeaderParser.parseCharset(httpResponse.headers))));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            default:
                return Response.error(new VolleyError("Error status code:" + httpResponse.statusCode));

        }
    }

    protected int parseOnErrorResponse(String responseBody) {
        try {
            JSONObject response = new JSONObject(responseBody);
            String moreInfo = response.getString("more_info");
            if (moreInfo.equals("Token was not recognised")) {
                return AuthTokenValidator.TOKEN_WAS_NOT_RECOGNISED;
            } else if (moreInfo.equals("Token has expired")) {
                return AuthTokenValidator.TOKEN_HAS_EXPIRED;
            } else if (moreInfo.equals("Client doesn't exist anymore")) {
                return AuthTokenValidator.CLIENT_DOES_NOT_EXIST_ANYMORE;
            } else if (moreInfo.equals("Client is locked")) {
                return AuthTokenValidator.CLIENT_IS_LOCKED;
            } else {
                return AuthTokenValidator.UNKNOWN_ERROR;
            }

        } catch (JSONException e) {
            e.printStackTrace();
            return AuthTokenValidator.UNKNOWN_ERROR;
        }

    }

    @Override
    protected void deliverResponse(Bundle response) {
        mListener.onGetUserDataResponse(response);
    }
}

实际上 params 参数现在是多余的

4

3 回答 3

10

getParams()不会在 GET 方法上调用,因此您似乎必须在发送请求之前将其添加到 URL。

查看 JavaDoc:

返回要用于 POST 或 PUT 请求的参数映射。

可以抛出 {@link AuthFailureError} 因为可能需要身份验证才能提供这些值。

请注意,您可以直接覆盖 {@link #getBody()} 以获取自定义数据。

@throws AuthFailureError 在身份验证失败的情况下

于 2013-08-28T14:24:05.790 回答
1

至于Itai Hanski的回答,这是实现这一点的一个例子:

 for(String key: params.keySet()) {
   url += "&"+key+"="+params.get(key);
 }
于 2015-09-09T08:17:55.880 回答
0

试试这个,

public class LoginRequest extends Request<String> {

    // ... other methods go here

    private Map<String, String> mParams;

    public LoginRequest(String param1, String param2, Listener<String> listener, ErrorListener errorListener) {
        super(Method.POST, "http://test.url", errorListener);
        mListener = listener;
        mParams.put("paramOne", param1);
        mParams.put("paramTwo", param2);

    }

    @Override
    public Map<String, String> getParams() {
        return mParams;
    }
}

另请参阅此示例,

https://github.com/evancharlton/folly/

于 2013-08-28T09:56:33.177 回答