android中服务器的请求错误无效

时间:2016-06-15 18:08:34

标签: php android json httpurlconnection

我正在尝试将凭据发送到我的服务器但我的PHP文件返回:

  

{ “错误”:[ “INVALID_REQUEST”]}

我的代码:

                JSONObject jsonObjectToSend = new JSONObject();
                jsonObjectToSend.put("email",credentials[0].toString());
                jsonObjectToSend.put("password",credentials[1].toString());

                String message = jsonObjectToSend.toString();

                HttpURLConnection conn = null;
                URL url = new URL("http://xx.xx.xx.xx/user/login.php");
                conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setFixedLengthStreamingMode(message.getBytes().length);
                conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
                conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
                conn.connect();
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                wr.write(message);
                wr.flush();
                BufferedReader reader = new BufferedReader (new InputStreamReader(conn.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line = null;

                //Getting the response from the server when we try to login.

                while((line = reader.readLine()) != null)
                {
                    sb.append(line);
                    break;
                }
                result = sb.toString();

我需要传递的JSON格式是:

{
 "email": "example@gmail.com",
 "password": "123456789" 
}

但我不知道在PHP脚本中使用了哪种方法(POST或GET)。我尝试了两种方法,但也遇到了同样的错误。

0 个答案:

没有答案