android volley post 参数,android – 使用volley post传递参数 -ag凯发k8国际
我能够使用postman和这些参数调用http端点:
{
"name":"val",
"subject":"test"
}
但是我无法通过android对volley做同样的事情:这里尝试使用jsonrequest:
hashmap params2 = new hashmap();
params.put("name", "val");
params.put("subject", "test subject");
jsonobjectrequest jsobjrequest = new jsonobjectrequest
(request.method.post, constants.cloud_url, new jsonobject(params2), new response.listener() {
@override
public void onresponse(jsonobject response) {
mview.showmessage("response: " response.tostring());
}
}, new response.errorlistener() {
@override
public void onerrorresponse(volleyerror error) {
// todo auto-generated method stub
mview.showmessage(error.getmessage());
}
});
// access the requestqueue through your singleton class.
volleysingleton.getinstance(mcontext).addtorequestqueue(jsobjrequest);
这里是尝试stringrequest
private void postmessage(context context, final string name, final string subject ){
requestqueue queue = volley.newrequestqueue(context);
stringrequest sr = new stringrequest(request.method.post, constants.cloud_url, new response.listener() {
@override
public void onresponse(string response) {
mview.showmessage(response);
}
}, new response.errorlistener() {
@override
public void onerrorresponse(volleyerror error) {
}
}){
@override
protected map getparams(){
map params = new hashmap();
params.put("name", name);
params.put("subject", subject);
return params;
}
@override
public map getheaders() throws authfailureerror {
map params = new hashmap();
params.put("content-type","application/x-www-form-urlencoded");
return params;
}
};
queue.add(sr);
}
当我使用jsonrequest时,调用post但没有传递参数,当我使用stringrequest时,我得到下面的错误?如何将json数据传递给volley调用?
e/volley: [13053] basicnetwork.performrequest: unexpected response code 400 for
这是处理请求的服务器代码
public static async task run(httprequestmessage req, tracewriter log)
{
var hellorequest = await req.content.readasasync();
var name = hellorequest?.name ?? "world";
var responsemessage = $"hello {persontogreet}!";
log.info($"message: {responsemessage}");
return req.createresponse(httpstatuscode.ok, $"all went well.");
}
public class hellorequest
{
public string name { get; set; }
public string subject { get; set; }
}
总结
以上是ag凯发k8国际为你收集整理的android volley post 参数,android – 使用volley post传递参数的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: lg g7 android9,随着and
- 下一篇: android 上传头像遇到的问题,an