Java 尝试将JSON对象发送到服务器时失败

Java 尝试将JSON对象发送到服务器时失败,java,android,Java,Android,我有一个JsonObject,我想从Android客户端将它发送到服务器 我正在学习一些教程和代码,但服务器总是收到一个空的post请求 这是我的代码: public String POST(String url, JSONObject object) { InputStream inputStream = null; String result = ""; try { // 1. create HttpClient HttpClie

我有一个JsonObject,我想从Android客户端将它发送到服务器

我正在学习一些教程和代码,但服务器总是收到一个空的post请求

这是我的代码:

public String POST(String url, JSONObject object) {

    InputStream inputStream = null;
    String result = "";

    try {

        // 1. create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // 2. make POST request to the given URL
        HttpPost httpPost = new HttpPost(url);

        String json = "";

        // 4. convert JSONObject to JSON to String
        json = object.toString();

        // 5. set json to StringEntity
        StringEntity se = new StringEntity(json);

        // 6. set httpPost Entity
        httpPost.setEntity(se);

        // 7. Set some headers to inform server about the type of the
        // content
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        // 8. Execute POST request to the given URL
        HttpResponse httpResponse = httpclient.execute(httpPost);

        // 9. receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // 10. convert inputstream to string
        if (inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

        Log.i("telo","respuesta: "+result);

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    // 11. return result
    return result;
}

private String convertInputStreamToString(InputStream inputStream)
        throws IOException {
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while ((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;

}
我做错了什么?一些帮助


谢谢

我认为您应该使用.toJSONString而不是.toString

请试试这个:

试试这个例子:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);

// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(1);
params.add(new BasicNameValuePair("object", object.toString()));

httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

if (entity != null) {
    InputStream instream = entity.getContent();
    try {
        // do something useful with instream
    } finally {
        instream.close();
    }
}

trye变更:内容类型,应用/json;到内容类型,application/json

你的json是空的!!!为什么?你指的是我的json字符串吗?否?分配后我的json字符串可能是空的,而不是空的。当httpclient带有responseHandler和entityutils时,为什么要使用convertInputStreamToString?嗯,这段代码来自教程,但现在我认为你是正确的,但这不是问题,因为我收到的答案是正确的,所以这个选项不出现。这个答案是错误的,因为toJSONString不是JSONObject的方法,而且,JSONObject的文档声明使用toString。我使用org.json.simple.JSONObject,它有这个函数!您使用的是什么库?android org.json.JSONObject中的默认库;请参阅本参考文件中的示例4。这正是你要找的。使用toJSONString ummm HttpClient无法识别此类将上述代码段中的第一行替换为HttpClient HttpClient=new DefaultHttpClient;。但问题还在继续…:这似乎很奇怪。。您是否检查过以确保您的api需要POST Json?问题可能就在那一边