【问题标题】:Volley jsonObjectRequest how to handle string resultVolley jsonObjectRequest 如何处理字符串结果
【发布时间】:2019-11-07 23:53:37
【问题描述】:

我正在尝试向页面发出 POST 请求,请求所需的参数必须在 json 中,因为它们采用这种格式:

{
   a: 1,
   b: "ASD",
   c: ["A","B"]
}

据我所知,这不能转换为Map<String, String> 对象,这是创建StringRequest 而不是JsonObjectRequest 所必需的,据说有什么方法可以发送@ 987654328@ 作为参数并且仍然能够处理来自服务器的字符串响应(使用 volley)?

我已经阅读了这些问题:

How to handle string response from php using android volley JsonObjectRequest [com.android.volley.ParseError: org.json.JSONException]?

-这个没有得到有效的答案

How to get string response from php using android volley JsonObjectRequest?

-这个没有回答我的问题

Volley - POST/GET parameters

-这个对自定义请求有一个有希望的答案,但它与我尝试的相反,我未能成功创建自己的自定义请求来接受我需要的参数。

【问题讨论】:

    标签: java android android-volley


    【解决方案1】:

    事实证明,在 hashmap 参数中发布 a[0]:"1" 和 a[1]:"2" 之类的内容与 a: ["1","2"] 相同,因此使用 StringRequest 很好,因为它使用 <String,String> Map 作为参数,据说我最终使用这个:

    StringRequest xx = new StringRequest(Request.Method.POST, getAjaxUrlForFunction("Login"), new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.w("RESPONSE",response);
                    callback.onSucces(response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                    callback.onError(error);
                }
            }){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String,String> paramss = new HashMap<String, String>();
                    paramss.put("funcion","login");
                    paramss.put("ajax_request","controller");
                    paramss.put("args[0]", name);
                    paramss.put("args[1]", password);
                    return paramss;
                }
            };
    

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 2013-05-22
      • 2021-07-11
      • 1970-01-01
      相关资源
      最近更新 更多