本文介绍了Volley JsonArrayRequest无法将参数发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SearchActivity.我必须将 search关键字之类的参数发送到服务器并接收JsonArray.

I have a SearchActivity . i must send a parameter like search keyword to server and receive JsonArray .

我将此代码用于JsonArrayRequest:

JsonArrayRequest stringRequest = new JsonArrayRequest(Request.Method.POST, url,
        new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                pDialog.dismiss();
                Log.e("response : ",response.toString());
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                pDialog.dismiss();
            }
        }){
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String,String> params = new Hashtable<String, String>();
        params.put("test", "ok");
        return params;
    }
};


AppController.getInstance().addToRequestQueue(stringRequest);

在服务器PHP文件中:

in server PHP file :

$keyword=  @$_POST["test"];
if($keyword=="ok")
{
echo'
[
   {
      "name":"name 1 ",
      "img":"1.jpg",
      "info":"hello",
      "id":"eee"
   },
   {
      "name":"name 2",
      "img":"2.jpg",
      "info":"hello",
      "id":"222"
   }
]
';
}

但是服务器不发送任何信息给应用程序

But server dont send anything for app

我通过StringRequest对其进行了测试,但效果很好,但在JsonArrayRequest中却无法工作

i tested it by StringRequest it work fine but in JsonArrayRequest it not work

当我使用JsonArrayRequest时,似乎参数未设置

When i use JsonArrayRequest it seem parameters not seted

请帮助

推荐答案

为什么不使用StringRequest?

如果您的回答是JsonArrayJsonObject,您也可以从StringResponse那里获得它.

If your response is JsonArray or JsonObject you can get it from StringResponse as well.

String response;

JSONArray arrayResponse = new JSONArray(response);

这篇关于Volley JsonArrayRequest无法将参数发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:47