2017-09-02 147 views
2

我送这样的如何使用Android中的Volley发送Post请求?

btnSignUp.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 


      //submitForm(); 
      JsonObjectRequest jsonobjectRequest = new JsonObjectRequest(Request.Method.POST, URL, null, new Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 
        //errorlabel.setText(response.toString()); 
       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

        errorlabel.setText("Invalid username/password"); 
       } 

      }){ 
       @Override 
       protected Map<String, String> getParams() throws AuthFailureError { 
        Map<String, String> params = new HashMap<String, String>(); 
        params.put("email", "[email protected]"); 
        params.put("password", "asd"); 



        return params; 
       } 
      }; 
      errorlabel.setText(jsonobjectRequest.toString()); 
      requestQueue.add(jsonobjectRequest); 

     } 
    }); 




} 

凌空请求,但我从服务器上说无效的电子邮件/密码,得到一个错误信息。

我已经设置了正确的参数。我在邮差上测试了它,并且在那里工作。这是一个截图。

截图

enter image description here

+0

你可以尝试用StringRequest? –

+0

JSONObject请求中存在一个错误,你不能发送参数:https://stackoverflow.com/questions/19837820/volley-jsonobjectrequest-post-request-not-working, 你需要尝试使用StringRequest,它肯定会工作。 –

+0

与字符串请求一起工作。如果你把它作为答案发布,我可以接受它。 – Nee

回答

0

我有同样的问题,我试着用字符串的要求,其工作

StringRequest jsonObjRequest = new StringRequest(Request.Method.POST, 
       URL, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 

        } 
       }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
      } 
     }) { 

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

      @Override 
      protected Map<String, String> getParams() throws AuthFailureError { 


       Map<String, String> postParam = new HashMap<String, String>(); 

       postParam.put("email", "[email protected]"); 
       postParam.put("password", "asd"); 


       return postParam; 
      } 

     }; 

     requestQueue.add(jsonObjRequest);