排球请求不参数

时间:2015-04-21 02:06:45

标签: android android-volley

我有一个自定义的截击请求,但在发送请求时它没有采用我的参数,我的代码有什么问题? 我在getParamsgetPostParams设置了断点,但没有一个断点。

我使用了从谷歌凌空分叉的com.mcxiaoke.volley:library:1.0.+来支持maven。

我发现这个课程在真实的设备上工作得很好,但不能在genymotion上工作。

public class GsonRequest<T> extends Request<T> {
  private Class<T> clazz;
  private Map<String, String> headers;
  private Map<String, String> params;
  private Listener<T> listener;

  public GsonRequest(Api api, Class<T> clazz, Listener<T> listener, ErrorListener errorListener) {
    this(api, clazz, null, null, listener, errorListener);
  }

  public GsonRequest(Api api, Class<T> clazz, Map<String, String> params, Listener<T> listener,
      ErrorListener errorListener) {
    this(api, clazz, params, null, listener, errorListener);
  }

  /**
   * Make a GET request and return a parsed object from JSON.
   *
   * @param url
   *            URL of the request to make
   * @param clazz
   *            Relevant class object, for Gson's reflection
   * @param headers
   *            Map of request headers
   */
  public GsonRequest(Api api, Class<T> clazz, Map<String, String> params, Map<String, String> headers,
      Listener<T> listener, ErrorListener errorListener) {
    super(api.method, api.url, errorListener);
    this.clazz = clazz;
    this.params = params;
    this.headers = headers;
    this.listener = listener;
  }

  // use new GsonRequest()
  @Deprecated
  public GsonRequest(String url, Class<T> clazz, Map<String, String> headers, Listener<T> listener,
      ErrorListener errorListener) {
    super(Method.GET, url, errorListener);
    this.clazz = clazz;
    this.headers = headers;
    this.listener = listener;
  }

  @Override
  public Map<String, String> getHeaders() throws AuthFailureError {
    return headers != null ? headers : super.getHeaders();
  }

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

  @Override
  public Map<String, String> getPostParams() throws AuthFailureError {   
    Map<String, String> result = params;
    return result;
  }

  @Override
  protected void deliverResponse(T response) {
    listener.onResponse(response);
  }

  @Override
  protected Response<T> parseNetworkResponse(NetworkResponse response) {
    try {
      String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers, "utf-8"));
//      Log.d("zhch", json);
      return Response.success(GsonUtils.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
      return Response.error(new ParseError(e));
    } catch (JsonSyntaxException e) {
      return Response.error(new ParseError(e));
    }
  }
}

1 个答案:

答案 0 :(得分:3)

此答案假设您正在尝试发出GET请求。

我有类似的问题。在使用Volley时传递参数时,GET请求与POST略有不同。  当你发出GET请求时,传递参数的方法之一就在url字符串本身内,这对我有用:

(这是一个部分示例,但应该为您提供修改自己代码所需的大部分内容)

在发送请求的类中,我使用了一个小方法将params附加到url:

//this method sits somewhere in your class
private String createGetWithParams(String url, Map<String, Object> params)
{
    StringBuilder builder = new StringBuilder();
    for (String key : params.keySet())
    {
        Object value = params.get(key);
        if (value != null)
        {
            try
            {
                value = URLEncoder.encode(String.valueOf(value), HTTP.UTF_8);
                if (builder.length() > 0)
                    builder.append("&");
                builder.append(key).append("=").append(value);
            }
            catch (UnsupportedEncodingException e)
            {
            }
        }
    }

    return (url += "?" + builder.toString());
}


//this method sits somewhere in the same class, this fires the request
public void doSomeRequest()
{
    Map<String, Object> jsonParams = new HashMap<>();
    jsonParams.put("SomeParam", SomeParamValue);
    jsonParams.put("SomeOtherParam", SomeOtherParamValue);

    String url = createGetWithParams("some/request/url", jsonParams);

    StringRequest request = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response)
                {
                 // do whatever
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error)
                {
                    if (null != error.networkResponse)
                    {
                        Log.d(" Volley Error Response code: ", ""+ error.networkResponse.statusCode);
                    }
                }
            });

      requestQueue.add(request);

我还创建了一个替换StringRequest的自定义请求类,但是它可以更好地控制解析响应 - 可能对你有所帮助,但是在这个类中我只覆盖了响应:

public class CustomStringRequest extends StringRequest
{
 private final Response.Listener<String> mListener;

 public CustomStringRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener)
 {
    super(method,url, listener, errorListener);
    mListener = listener;
 }

 @Override
 protected Response<String> parseNetworkResponse(NetworkResponse response)
 {
     try
     {
         // response.data is the byte array, do whatever..
         String responseBody = new String(response.data, "utf-8");
         Log.d(" NetworkResponse", responseBody);

         return (Response.success(responseBody, getCacheEntry()));
     }
     catch (UnsupportedEncodingException e)
     {
         VolleyLog.e("UnsupportedEncodingException");
         Log.d("NetworkResponse Exception", e.getMessage() );
         return (null);
     }
 }

 @Override
 protected void deliverResponse(String response)
 {
     mListener.onResponse(response);
 }

}

我知道的另一种方式是使用特定的http客户端,我没有用过这种方式,但是你可以使用OkHttp或类似的东西。

希望这有帮助!