解决方案 »

  1.   

    看看这个:        // JSONObject data, String url HttpPost request = new HttpPost(url); StringEntity entity; entity = new StringEntity(data.toString());
    request.addHeader("Content-Type", "application/json");
    request.setEntity(entity);
    // 发送请求
    HttpResponse httpResponse = new DefaultHttpClient().execute(request);
      

  2.   

    首先,谢谢大哥的提示,参考上面的代码我改了一下,那个问题解决了,但是随即又出现另外一个问题,依然让我一头雾水。描述如下:
    我参考你提供的代码将发送Post请求飞方法改成
    public static String postRequest(String url, JSONObject jsonObject)
    throws Exception {
    // 创建HttpPost对象。
    HttpPost post = new HttpPost(url);
    // 设置请求参数
    StringEntity entity = new StringEntity(jsonObject.toString());
    post.setEntity(entity);
    post.setHeader("Accept", "application/json");
    post.setHeader("Content-Type", "application/json");
    // 发送POST请求
    HttpResponse httpResponse = httpClient.execute(post);
    // 如果服务器成功地返回响应
    if (httpResponse.getStatusLine().getStatusCode() == 200) {
    // 获取服务器响应字符串
    String result = EntityUtils.toString(httpResponse.getEntity());
    return result;
    }
    return null;
    }
    现在
    httpResponse.getStatusLine().getStatusCode()返回状态200没有问题,有问题的是进入if语句后EntityUtils.toString(httpResponse.getEntity());却抛了异常。抛出的异常是:java.lang.IllegalStateException: Content has been consumed。
    我网上查了这个异常,原因是指httpResponse.getEntity()只能被读取一次。但是上面的代码中httpResponse.getEntity()只是被调了一次呀,为什么还会出现这个异常呢。不解中,希望得到指点。
      

  3.   

    public static JSONObject response2Json(HttpResponse httpresponse) throws IOException, JSONException {
        HttpEntity resultentity = httpresponse.getEntity();
        InputStream inputstream = resultentity.getContent();
        String resultstring = convertStreamToString(inputstream);
        inputstream.close();
        Log.d(TAG, "result = " + resultstring);
        return new JSONObject(resultstring);
      }
      

  4.   

    代码应该没有问题的,唯一不一样的地方是我的代码中是new DefaultHttpClient(),而你的是httpClient,是一个保存下来的,多次调用时出现你描述的错误吧。你改过来再试一试。
      

  5.   

    楼主,我想问个关于REST服务的问题!我自己用VS2010搭建了一个简单的REST服务,里边有GET、POST、PUT和DELETE四种请求服务,通过POST请求调用REST服务的时候,传递参数用一个List来做,但是参数名称总是不对,不知道该怎么解决了,希望你能听懂,帮帮忙