凌空要求不采取参数参数

2023-09-13 02:07:46 作者:看不见的坚强

我有一个自定义凌空请求,但发送请求时,它没有把我的参数,可以有什么错我的code? 我设置一个断点 getParams能够 getPostParams ,但它们都没有经历过。

我用 com.mcxiaoke.volley:图书馆:1.0 + 这是从谷歌凌空叉,支持Maven的

我找到该类运作良好,在真实的设备,但水湿工作在genymotion。

 公共类GsonRequest< T>扩展请求< T> {
  私有类< T> clazz所;
  私人地图<字符串,字符串>头;
  私人地图<字符串,字符串> PARAMS;
  私人监听器< T>侦听器;

  公共GsonRequest(原料药,类< T> clazz中,收听LT; T>听者,ErrorListener errorListener){
    这个(API,clazz中,NULL,NULL,监听器,errorListener);
  }

  公共GsonRequest(原料药,类< T> clazz中,地图<字符串,字符串>参数,可以收听LT; T>听者,
      ErrorListener errorListener){
    这个(API,clazz中,参数,可以为null,监听器,errorListener);
  }

  / **
   *做一个GET请求,并从JSON返回解析的对象。
   *
   * @参数的URL
   *请求的URL,使
   * @参数clazz所
   *相关类对象,对于GSON的反思
   * @参数头
   *地图请求头
   * /
  公共GsonRequest(原料药,类< T> clazz中,地图<字符串,字符串> PARAMS,地图<字符串,字符串>头,
      收听LT; T>监听器,ErrorListener errorListener){
    超(api.method,api.url,errorListener);
    this.clazz = clazz所;
    this.params = PARAMS;
    this.headers =头;
    this.listener =侦听器;
  }

  //使用新GsonRequest()
  pcated @德$ P $
  公共GsonRequest(字符串URL,类< T> clazz中,地图<字符串,字符串>头,监听器< T>听者,
      ErrorListener errorListener){
    超(Method.GET,网址,errorListener);
    this.clazz = clazz所;
    this.headers =头;
    this.listener =侦听器;
  }

  @覆盖
  公共地图<字符串,字符串> getHeaders()抛出AuthFailureError {
    返回头!= NULL?标题:super.getHeaders();
  }

  @覆盖
    公共地图<字符串,字符串> getParams()方法抛出AuthFailureError {
    地图<字符串,字符串>结果= PARAMS;
    返回结果;
    }

  @覆盖
  公共地图<字符串,字符串> getPostParams()抛出AuthFailureError {
    地图<字符串,字符串>结果= PARAMS;
    返回结果;
  }

  @覆盖
  保护无效deliverResponse(T响应){
    listener.onResponse(响应);
  }

  @覆盖
  受保护的响应< T> parseNetworkResponse(NetworkResponse响应){
    尝试 {
      JSON字符串=新的String(response.data,HttpHeaderParser.parseCharset(response.headers,UTF-8));
// Log.d(zhch,JSON);
      返回Response.success(GsonUtils.fromJson(JSON,clazz所),HttpHeaderParser.parseCacheHeaders(响应));
    }赶上(UnsupportedEncodingException E){
      返回Response.error(新ParseError(e)条);
    }赶上(JsonSyntaxException E){
      返回Response.error(新ParseError(e)条);
    }
  }
}
 

解决方案

这答案假设你正试图使一个GET请求。

我也有类似的问题。 GET请求比POST有点不同,当使用时,齐射来传递参数。  当你做一个GET请求,传递PARAMS的方法之一是URL字符串本身中,这个工作对我来说:

(这是一个局部的例子,但应该给你最什么,你需要修改自己的code)

怎么参数不一致

在发送我用一个小方法追加PARAMS到URL请求的类:

  //此方法在您的类坐在某处
私人字符串createGetWithParams(字符串URL,地图<字符串,对象> PARAMS)
{
    StringBuilder的建设者=新的StringBuilder();
    对于(字符串键:params.keySet())
    {
        对象值= params.get(密钥);
        如果(值!= NULL)
        {
            尝试
            {
                值= URLEn coder.en code(将String.valueOf(值),HTTP.UTF_8);
                如果(builder.length()大于0)
                    builder.append(与&);
                。builder.append(键).append(=)追加(值);
            }
            赶上(UnsupportedEncodingException E)
            {
            }
        }
    }

    返程(URL + = + builder.toString()?);
}


//此方法在同一个班坐的地方,这触发请求
公共无效doSomeRequest()
{
    地图<字符串,对象> jsonParams =新的HashMap<>();
    jsonParams.put(SomeParam,SomeParamValue);
    jsonParams.put(SomeOtherParam,SomeOtherParamValue);

    字符串URL = createGetWithParams(有些/请求/ URL,jsonParams);

    StringRequest请求=新StringRequest(Request.Method.GET,网址,
            新Response.Listener<字符串>()
            {
                @覆盖
                公共无效onResponse(字符串响应)
                {
                 //做什么
                }
            },
            新Response.ErrorListener()
            {
                @覆盖
                公共无效onErrorResponse(VolleyError错误)
                {
                    如果(NULL!= error.networkResponse)
                    {
                        Log.d(排球错误响应code:,+ error.networkResponse.status code);
                    }
                }
            });

      requestQueue.add(要求);
 

我还创建了取代StringRequest一个自定义的请求类,但是这是为了更好地控制解析响应 - 可以帮助你,虽然,在这个类我只覆盖了回应:

 公共类CustomStringRequest扩展StringRequest
{
 私人最终Response.Listener<字符串> mListener;

 公共CustomStringRequest(INT方法,字符串URL,Response.Listener<字符串>听者,Response.ErrorListener errorListener)
 {
    超(方法,URL,监听器,errorListener);
    mListener =侦听器;
 }

 @覆盖
 受保护的响应和LT;字符串> parseNetworkResponse(NetworkResponse响应)
 {
     尝试
     {
         // response.data是字节数组,做什么..
         字符串responseBody =新的String(response.data,UTF-8);
         Log.d(NetworkResponse,responseBody);

         返程(Response.success(responseBody,getCacheEntry()));
     }
     赶上(UnsupportedEncodingException E)
     {
         VolleyLog.e(UnsupportedEncodingException);
         Log.d(NetworkResponse异常,e.getMessage());
         返程(空);
     }
 }

 @覆盖
 保护无效deliverResponse(字符串响应)
 {
     mListener.onResponse(响应);
 }
 

}

我知道是使用特定的HTTP客户端的其他方式,我没有使用过这种方式,但你很可能使用OkHttp,或者类似的东西。

希望这有助于!

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

解决方案

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!