Android - Volley post参数添加了额外的'&'

时间:2015-05-08 03:36:31

标签: android http networking android-volley

我正在将我的应用程序转换为使用Volley框架。

据我所知,当我使用HttpClient发出web服务请求时,我已经完成了所有工作,wireshark正在显示我的参数

  

devicePrint = Android和安培; mobileSaveUserId =假安培; __的responseType = XML&安培; appVersion = Android的3.2&安培; __ ResponseTypeVersion = 1.0&安培;用户id = mbltest6&安培; retrievePostedTransaction = falseHTTP / 1.1   200 OK

这很有效,但是当我使用凌空时,我的数据包就像这样。

  

devicePrint = Android和安培; mobileSaveUserId =假安培; __的responseType = XML&安培; appVersion = Android的3.2&安培; __ ResponseTypeVersion = 1.0&安培;用户ID = mbltest6&安培; retrievePostedTransaction =假安培; HTTP / 1.1   200 OK

我将它们并排移动并注意到凌空的例子(底部的一个)附加了额外的"&"这导致我的web服务返回"未找到用户名"即使它显然在那里。

有关如何解决此问题的任何建议?或者它为什么会发生?

编辑:在对基类进行进一步调查后,当您设置参数时,它总是会添加额外的"&"到字符串的末尾。这是凌空的正常行为吗?这可能导致我的请求失败吗?

private byte[] encodeParameters(Map<String, String> params, String paramsEncoding) {
    StringBuilder encodedParams = new StringBuilder();

    try {
        Iterator uee = params.entrySet().iterator();

        while(uee.hasNext()) {
            java.util.Map.Entry entry = (java.util.Map.Entry)uee.next();
            encodedParams.append(URLEncoder.encode((String)entry.getKey(), paramsEncoding));
            encodedParams.append('=');
            encodedParams.append(URLEncoder.encode((String)entry.getValue(), paramsEncoding));
            encodedParams.append('&');
        }

        return encodedParams.toString().getBytes(paramsEncoding);
    } catch (UnsupportedEncodingException var6) {
        throw new RuntimeException("Encoding not supported: " + paramsEncoding, var6);
    }
}

我的代码如下:

public abstract class WebServiceRequest extends StringRequest {

private static final int SOCKET_TIMEOUT = (1000 * 60);

private static final String RESPONSE_TYPE_KEY = "__ResponseType";
private static final String RESPONSE_TYPE_VERSION_KEY = "__ResponseTypeVersion";
private static final String APP_VERSION_KEY = "appVersion";
private static final String RETRIEVE_POSTED_KEY = "retrievePostedTransaction";

public boolean mRetrievePostedTransactions;
private Map<String, String> mParameters;

public WebServiceRequest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
    super(Method.POST, url, listener, errorListener);
    this.mParameters = new HashMap<>();
    setRetryPolicy(new DefaultRetryPolicy(
            SOCKET_TIMEOUT,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
    ));
}


public abstract void setParams(Map<String,String> params);


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

private Map<String, String> setCommonParameters (Map<String,String> params) {
    params.put( APP_VERSION_KEY, "Android-" + VirtualWalletApplication.getInstance().appVersion );
    params.put( RESPONSE_TYPE_KEY, "XML" );
    params.put( RESPONSE_TYPE_VERSION_KEY, "1.0" );
    params.put( RETRIEVE_POSTED_KEY, Boolean.valueOf( mRetrievePostedTransactions ).toString() );
    return params;
}

}

这是我创建请求的地方

 WebServiceRequest request = new WebServiceRequest(
            StringUtils.getFullServerUrlForResource("/alservlet/ValidateUserIdServlet"),
            onSuccessListener,
            onFailureListener) {

        @Override
        public void setParams(Map<String, String> params) {
            params.put("userid","mbltest6");
            params.put("mobileSaveUserId","false");
            params.put("devicePrint","Android");
        }

    };

    NetworkVolley.getInstance().sendRequest(request);

1 个答案:

答案 0 :(得分:0)

原来,我需要将参数“userId”大写为“userid”。密切关注你的参数人员!