gpt4 book ai didi

带有 Map params (?) 的 Android Volley Post

转载 作者:太空狗 更新时间:2023-10-29 15:04:34 28 4
gpt4 key购买 nike

我一直在尝试发布我的 Credentials从 Android 到 C#.Net Web 服务器的类。

Volley Post 方法接受如下参数:

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

getParams() 的返回类型是Map<String, String>但我需要Map<String, Object>将我的类(class)发送到网络服务器。我什至尝试将我的类转换为 json 字符串,例如:

@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("credentials", new Gson().toJson(mCredentials, Credentials.class));
return parameters;
}

但它不起作用。服务器返回“credentials”参数为 null 时抛出的“Invalid Parameter”错误。

服务器端没有任何问题,因为我可以用 AsyncTask 做到这一点.我决定将我的请求变成 Volley我陷入了这个问题。

谁有解决办法?

最佳答案

我是这样做的(注意:params 是一个 jsonobject,而不是一个 map):

 //fill params
JSONObject params = new JSONObject();
params.put("username", username);
params.put("password", password);

//create future request object
RequestFuture<JSONObject> future = RequestFuture.newFuture();
//create JsonObjectRequest
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, URL, params, future, future);
//add request to volley
MyVolley.getRequestQueue().add(jsObjRequest);
//pop request off when needed
try {
JSONObject response = future.get();//blocking code
} catch (Exception e) {
e.printStackTrace();
}

关于带有 Map<String, Object> params (?) 的 Android Volley Post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23251184/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com