Java 使用URLConnection从android发送JSON时的代码400

Java 使用URLConnection从android发送JSON时的代码400,java,android,json,Java,Android,Json,当我试图将数据发送到服务器时,代码为400。这里是调用post方法的异步任务。当我使用get方法时,它正确显示结果,但使用post时,它会导致连接错误 private class PutData extends AsyncTask<Object , Void , String> { String habitsUrl = "http://donow-app.herokuapp.com/habits.json"; int responseCode = -1; St

当我试图将数据发送到服务器时,代码为400。这里是调用post方法的异步任务。当我使用get方法时,它正确显示结果,但使用post时,它会导致连接错误

private class PutData extends AsyncTask<Object , Void , String>
{
    String habitsUrl = "http://donow-app.herokuapp.com/habits.json";
    int responseCode = -1;
    String responseData;
    @Override
    protected String doInBackground(Object... params)
    {
        try
        {
            URL habits = new URL(habitsUrl);
            HttpURLConnection connection = (HttpURLConnection) habits.openConnection();
            connection.addRequestProperty("x-user-email" , email);
            connection.addRequestProperty("x-user-token" , "WieLmf2DVKpsAPzgN7HA");
            connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            connection.setRequestMethod("POST");
            connection.connect();
            //connection.setDoOutput(true);
            //connection.setUseCaches(true);
            responseCode = connection.getResponseCode();
            if(responseCode == HttpURLConnection.HTTP_OK)
            {
                JSONObject jsonParam = new JSONObject();
                jsonParam.put("habit[title]", "Hello");
                Log.v(TAG , jsonParam.toString());
                DataOutputStream printout = new DataOutputStream(connection.getOutputStream());
                //Log.v(TAG, (jsonParam.toString()));
                System.out.print(jsonParam.toString());
                printout.write(jsonParam.toString().getBytes());
                printout.flush();
                printout.close();
            }
            else
            {
                Log.i(TAG , "Code" + responseCode);
            }


        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return responseData;
    }

}
私有类PutData扩展异步任务
{
字符串habitsUrl=”http://donow-app.herokuapp.com/habits.json";
int-responseCode=-1;
字符串响应数据;
@凌驾
受保护的字符串doInBackground(对象…参数)
{
尝试
{
URL习惯=新URL(habitsUrl);
HttpURLConnection连接=(HttpURLConnection)habits.openConnection();
connection.addRequestProperty(“x-user-email”,email);
connection.addRequestProperty(“x-user-token”、“WieLmf2DVKpsAPzgN7HA”);
connection.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
connection.setRequestMethod(“POST”);
connection.connect();
//connection.setDoOutput(真);
//connection.setUseCaches(true);
responseCode=connection.getResponseCode();
if(responseCode==HttpURLConnection.HTTP\u确定)
{
JSONObject jsonParam=新的JSONObject();
jsonParam.put(“习惯[标题],“你好”);
Log.v(标记,jsonParam.toString());
DataOutputStream打印输出=新的DataOutputStream(connection.getOutputStream());
//Log.v(标记,(jsonParam.toString());
System.out.print(jsonParam.toString());
write(jsonParam.toString().getBytes());
printout.flush();
printout.close();
}
其他的
{
Log.i(标签,“代码”+响应代码);
}
}
捕获(格式错误)
{
e、 printStackTrace();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
捕获(例外e)
{
e、 printStackTrace();
}
返回响应数据;
}
}
请帮助我删除此错误。我已经尝试了所有的可能性,但没能找出我的错误。
提前谢谢

这不是你的Android代码的问题。您的服务器有问题。当您向该端点发送POST请求时,会发生什么情况?我正在尝试将“hello”值发送到数据库中“习惯”的键“title”。