将POST参数添加到Android Volley JsonObjectRequest

将POST参数添加到Android Volley JsonObjectRequest

问题描述:

大家下午好吧 我对我的本地服务器进行了抽象连接。事实证明,连接工作正常,但我的参数不被我的MysqlPHP脚本接受。 我相信参数没有正确发送。 这里是代码将POST参数添加到Android Volley JsonObjectRequest

try { 

     RequestQueue jr = Volley.newRequestQueue(this); 

     HashMap<String, String> params = new HashMap<String, String>(); 
     params.put("username", username); 
     params.put("password", password); 
     Log.d("The paramet ready", "Ready to go"); 

     JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(params), 
       new Response.Listener<JSONObject>() { 

        @Override 
        public void onResponse(JSONObject response) { 
         Log.d("The response", response.toString()); 
         progressDial.hide(); 
         JSONArray json = null; 
         try { 
          json = response.getJSONArray("result"); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 

         try { 
          if (json.getString(0).equalsIgnoreCase("0")) { 
           Log.d("JsonString: -> ", json.toString()); 
           progressDial.hide(); 
           toast(); 

          } else { 

           startagain(); 
          } 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       error.printStackTrace(); 
       progressDial.hide(); 

      } 
     } 
     ); 
     jr.add(jsonObject); 
+0

你的服务器希望如何接收参数?像这样发送发布参数会使帖子主体以JSON格式显示,这可能与您的服务器实现不兼容。 –

+0

@GilMoshayof好的,谢谢。我如何使它兼容? –

+0

@GilMoshayof,使用GET对我来说工作得很好。非常感谢你的贡献 –

我遇到了类似的问题。我有一个服务器API返回一个JSON对象响应,所以JsonObjectRequest是前往请求类型,但服务器不喜欢我的身体是JSON格式,所以我不得不对我的请求做一些更改。

这里就是我所做的(适用于您的代码):

JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.POST, url, null, 
      new Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 
        Log.d("The response", response.toString()); 
        progressDial.hide(); 
        JSONArray json = null; 
        try { 
         json = response.getJSONArray("result"); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

        try { 
         if (json.getString(0).equalsIgnoreCase("0")) { 
          Log.d("JsonString: -> ", json.toString()); 
          progressDial.hide(); 
          toast(); 

         } else { 

          startagain(); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      error.printStackTrace(); 
      progressDial.hide(); 

     } 
    } 
    ) 
    { 
     @Override 
     public byte[] getBody() 
     { 
      try 
      { 
       final String body = "&username=" + username + // assumes username is final and is url encoded. 
            "&password=" + password // assumes password is final and is url encoded. 
       return body.getBytes("utf-8"); 
      } 
      catch (Exception ex) { } 

      return null; 
     } 

     @Override 
     public String getBodyContentType() 
     { 
      return "application/x-www-form-urlencoded"; 
     } 

     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError 
     { 
      Map<String, String> headers = new HashMap<String, String>(); 

      headers.put("Accept", "application/json"); 

      return headers; 
     } 


    }; 

在这里,我不发送任何JSON对象作为后身体,而是,我创建了后身体我自己,表单url编码。

我重写了以下方法:

  1. getBody - 我创建后的身体完全服务器希望它的方式 - 形式的URL编码。
  2. getBodyContentType - 我告诉服务器我的身体的内容类型是什么
  3. getHeaders - 我告诉服务器以JSON格式返回结果。这可能不是你需要的。
+0

我真的不明白。有没有其他方法? –

+0

您是否尝试过我发布的解决方案? –

+0

是的,我有。没有工作,也许我做得不好。我不能只使用GET而不是POST? –

如果你的API返回一个JSON数组,那么你应该使用JsonArrayRequest而不是JsonRequest。