使用volley

时间:2015-07-05 12:25:07

标签: android url post android-volley urlencode

服务器接受字符串参数在制作json对象请求时(通过post方法)使用volley ,同样在自定义截击请求中它是需要在getParams()方法中传递一个字符串哈希映射我有字符串以及要在那里传递的长值,还尝试了 Uri 类,但也有appendQueryParameter()中需要sting。我使用了以下链接Link 1 Link 2 Link 3

Volley class

公共类CustomRequest扩展了Request {

private Listener<JSONObject> listener;
private Map<String, String> params;

public CustomRequest(String url, Map<String, String> params,
                     Listener<JSONObject> reponseListener, ErrorListener errorListener) {
    super(Method.GET, url, errorListener);
    this.listener = reponseListener;
    this.params = params;
}

public CustomRequest(int method, String url, Map<String, String> params,
                     Listener<JSONObject> reponseListener, ErrorListener errorListener) {
    super(method, url, errorListener);
    this.listener = reponseListener;
    this.params = params;
}

protected Map<String, String> getParams()
        throws com.android.volley.AuthFailureError {
    return params;
}


@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers));
        return Response.success(new JSONObject(jsonString),
                HttpHeaderParser.parseCacheHeaders(response));
       /* return Response.success(
                gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));*/
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<>();
    String loginEncoded = new String(Base64.encode(("uictester:?f!T!ziX}.,(").getBytes(), Base64.DEFAULT));
    headers.put("Authorization", "Basic " + loginEncoded);
    return headers;
}

@Override
protected void deliverResponse(JSONObject response) {
    // TODO Auto-generated method stub
    listener.onResponse(response);
}

}

1 个答案:

答案 0 :(得分:1)

首先需要使用String.valueOf(long value)方法将long参数转换为String,然后使用其键将此参数放入hashmap。为:

params.put(&#34; key&#34;,String.valueOf(long value));