自定义排球请求类始终返回401

时间:2016-05-15 11:18:09

标签: android android-volley

我有这个类扩展了Volley的StringRequest

public abstract class CustomRequest extends StringRequest {

private static final String TAG = CustomRequest.class.getSimpleName();

private Response.Listener<String> listener;
private HashMap<String, String> bodyParams, headerParams;

public CustomRequest(int method, String url, Response.Listener<String> responseListener,
                     Response.ErrorListener errorListener) {
    super(method, url, responseListener, errorListener);
    setTag(this.getClass().getSimpleName());
    this.listener = responseListener;
    this.bodyParams = new HashMap<>();
    this.headerParams = new HashMap<>();
    addHeaderParam("token", Profile.getInstance().getToken());
}

public void start(){
    Log.i(this.getClass().getSimpleName(), "Request parameters---------------------->");
    Log.d(this.getClass().getSimpleName(), "URL = " + getUrl());
    Log.d(this.getClass().getSimpleName(), "headers = " + logParams(headerParams));
    Log.d(this.getClass().getSimpleName(), "body = " + logParams(bodyParams));
    Log.d(this.getClass().getSimpleName(), "method = " + getMethodAsString());
    Log.i(this.getClass().getSimpleName(), "Starting request---------------------");
    VolleyInstance.getInstance().addToRequestQueue(this);
}

private String getMethodAsString() {
    switch (getMethod()){
        case Method.DELETE:
            return "delete";
        case Method.DEPRECATED_GET_OR_POST:
            return "deprecated get or post";
        case Method.GET:
            return "get";
        case Method.HEAD:
            return "head";
        case Method.OPTIONS:
            return "options";
        case Method.PATCH:
            return "patch";
        case Method.POST:
            return"post";
        case Method.PUT:
            return "put";
        case Method.TRACE:
            return "trace";
        default:
            return "method not found";
    }
}

private String logParams(Map<String, String> params){
    Iterator it = params.entrySet().iterator();
    StringBuilder sb = new StringBuilder();
    sb.append("{");
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        if(sb.length() != 1){
            sb.append(", ");
        }
        sb.append(pair.getKey()).append(" = ").append(pair.getValue());
    }
    sb.append("}");
    return sb.toString();
}

protected void addHeaderParam(String key, String value){
    headerParams.put(key, value);
}

protected void addBodyParam(String key, String value){
    bodyParams.put(key, value);
}

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

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

@Override
protected void deliverResponse(String response) {
    Log.d(this.getClass().getSimpleName(), "Response = " + response.toString());
    listener.onResponse(response);
}

@Override
public void deliverError(VolleyError error) {
    super.deliverError(error);
    error.printStackTrace();
    if(!(error instanceof NoConnectionError)){
        Crashlytics.logException(error);
    }

}

我像这样扩展了CustomRequest:

public class GetLastFiveUpdatedLists extends CustomRequest {

private static final String URL_ADDITION = "/v1/list/lastFiveList";

public GetLastFiveUpdatedLists(Response.Listener<String> responseListener, Response.ErrorListener errorListener) {
    super(Method.GET, Global.URL + URL_ADDITION, responseListener, errorListener);
}

我称之为:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // code
    new GetLastFiveUpdatedLists(this.generateResponseListener(), this.generateErrorListener()).start();
    // code
    return rootView;
}

问题是我总是得到一个BasicNetwork.performRequest:http://myurl.com的意外响应代码401。我知道API正在运行,因为它在iOS上完美运行。

我还尝试扩展JSONObjectRequest而不是StringRequest,并覆盖getHeaders(),但结果相同。

2 个答案:

答案 0 :(得分:0)

401错误表示请求未经授权。这很可能意味着您的令牌可能无效。尝试记录 Profile.getInstance()。getToken()以确认它是否是您认为的,您还可以使用rest客户端检查网络响应。 根据你的说法,“不需要标题的请求(例如登录)工作完全正常”这在很大程度上支持了我的主张。

答案 1 :(得分:0)

我解决了。我用“授权”替换了“令牌”。这节课工作得很好。