Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

android - Volley request not taking parameters

I have a custom volley request, but it didn't take my params when sending request, what's wrong with my code? I set a breakpoint at getParams and getPostParams, but none of them went through.

I used com.mcxiaoke.volley:library:1.0.+ which is forked from google volley to support maven.

I find the class worked well at real device but cann't work at 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));
    }
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This answer assumes you are trying to make a GET request.

I had a similar issue. GET requests are a little different than POST when it comes to passing parameters when using Volley. when you make a GET request, ONE of the WAYS to pass the params is inside the url string itself, this worked for me :

(this is a partial example, but should give you most of what you need to modify your own code)

In the class that sends the requests I used a small method to append the params to the 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);

I Also created a custom request class that replaced the StringRequest, but that was to have more control over parsing the response - might help you though, in this class I only override the response:

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

}

the other way I know of is using a specific http client, I haven't used that way, but you could probably use OkHttp, or something similar.

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...