如何使用Android通过HttpClient Request发送JSON对象?

Sac*_*ani 14 post android json request

我想将JSON文本{}发送到Web服务并阅读响应.我怎么能从android做到这一点?有哪些步骤,例如创建请求对象,设置内容标题等.

我的代码在这里

public void postData(String result,JSONObject obj) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, 10000);
    HttpConnectionParams.setSoTimeout(myParams, 10000);

    String json=obj.toString();

    try {

        HttpPost httppost = new HttpPost(result.toString());
        StringEntity se = new StringEntity(obj.toString()); 
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppost.setEntity(se); 

        HttpResponse response = httpclient.execute(httppost);
        String temp = EntityUtils.toString(response.getEntity());
        Log.i("tag", temp);


    } catch (ClientProtocolException e) {

    } catch (IOException e) {
    }
}
Run Code Online (Sandbox Code Playgroud)

我做了什么错误plz纠正我,因为它显示我一个错误的请求错误,但当我在海报发布时,它显示我的状态为Successfull 200 ok

cur*_*zen 13

我这样做

httppost.setHeader("Content-type", "application/json");
Run Code Online (Sandbox Code Playgroud)

此外,new HttpPost()将Web服务URL作为参数.