【问题标题】:How to send POST request in JSON and response in JSON using volley (Android)如何使用 volley (Android) 以 JSON 格式发送 POST 请求并以 JSON 格式响应
【发布时间】:2016-03-16 12:09:53
【问题描述】:
    private void login(){
   // Post params to be sent to the server
   Map<String, String> params = new HashMap<String, String>();
   params.put("user_id", username);
   params.put("password", password);


   JsonObjectRequest req = new JsonObjectRequest(login_URL, new JSONObject(params),
           new Response.Listener<JSONObject>() {
               @Override
               public void onResponse(JSONObject response) {
                   VolleyLog.v("Response:%n %s", response.toString());
               }
           }, new Response.ErrorListener() {
       @Override
       public void onErrorResponse(VolleyError error) {
           VolleyLog.e("Error: ", error.getMessage());
       }
   }){
       /**
        * Passing some request headers
        * */
       @Override
       public Map<String, String> getHeaders() throws AuthFailureError {
           HashMap<String, String> headers = new HashMap<String, String>();
           headers.put("Content-Type", "application/json; charset=utf-8");
           return headers;
       }

   };
// add the request object to the queue to be executed
   AppController.getInstance().addToRequestQueue(req);}

我收到以下错误: E/Volley:[1] 10.onErrorResponse:错误:

不知道哪里出了问题

json 作为邮件发送: { "":"", "":"", "":"", } json 响应为: {"":""}

【问题讨论】:

  • 详细地发布你的错误。
  • 只是“E/Volley: [1] 10.onErrorResponse: Error:”
  • 但没有描述错误。只是错误吗?使用日志获取错误..
  • 关于json格式的response,服务器必须返回json格式的response
  • 我只是注意到它正在工作,但有时返回“E/Volley:[1] 10.onErrorResponse:错误:”或“D/Volley:[218] BasicNetwork.logSlowRequests:HTTP 响应请求=

标签: android json post android-volley


【解决方案1】:

您应该添加 Request.Method.POST 作为新 JsonObjectRequest 方法的第一个参数。同时删除新的 'JSONObject(params)'。

现在覆盖 getParams 覆盖上方的 getParams() 方法,并在那里添加登录方法的内容。你应该返回参数。

它应该工作。如果您有任何挑战,请告诉我。 :)。

一个很好的例子是here

【讨论】:

    【解决方案2】:

    可以使用评分器集成 Volley 库,如果您想了解 volley 中请求的实现,那么下面的链接可以为您提供您可以使用 volley 提出的所有类型的请求以及如何实现它。

    给你

    http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

    【讨论】:

      猜你喜欢
      • 2012-04-02
      • 2014-04-10
      • 2011-09-07
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 2022-02-21
      • 1970-01-01
      相关资源
      最近更新 更多