使用凌空在URL请求中传递Long参数的方法 [英] Way to pass Long parameter in url request using volley

查看:1077
本文介绍了使用凌空在URL请求中传递Long参数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器不接受字符串参数(在使用齐射通过排球方式发出JSON对象请求时)(在自定义齐射请求中) 需要在getParams()方法中传递字符串哈希映射.我要在其中传递字符串和长值,也可以通过 Uri 类进行尝试,但也有在appendQueryParameter()中需要字符串.我在下面使用了链接链接1 链接2

Server is not accepting String parameters when making json object request (by post method) using volley, also in custom volley request it is required to pass a string Hash map in getParams() method i am having string as well as long values to be pass over there, Also tried by Uri class but there is also sting is required in appendQueryParameter(). i have used below links Link 1 Link 2 Link 3

Volley class

公共类CustomRequest扩展了请求{

public class CustomRequest extends 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);
}

}

推荐答案

您首先需要使用String.valueOf(long value)方法将long参数转换为String,然后将此参数及其键放入哈希表中.为:

You first need to convert the long parameter to String using String.valueOf(long value) method and then put this param to the hashmap with its key. as:

params.put("key",String.valueOf(long value));

params.put("key", String.valueOf(long value));

这篇关于使用凌空在URL请求中传递Long参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆