我想获得一个http返回类型的http响应我得到的http post请求i触发器

时间:2015-06-19 11:37:07

标签: java

我想得到一个http响应的字符串返回类型我得到的http post请求i触发器,但我的返回变量给出错误

  

"消息无法解析为变量"

随后我必须将返回字符串值转换为json字符串数据。

这是我的代码......

public String sendPOST(String _postData) {

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(POST_URL);
    httpPost.addHeader("User-Agent", USER_AGENT);

    StringEntity entity = new StringEntity(_postData, HTTP.UTF_8);
    entity.setContentType("application/json");
    httpPost.setEntity(entity);

    CloseableHttpResponse httpResponse;
    try {
        httpResponse = httpClient.execute(httpPost);

        System.out.println("POST Response Status:: " + httpResponse.getStatusLine().getStatusCode());

        BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));

        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = reader.readLine()) != null) {
            response.append(inputLine);

            String message = org.apache.commons.io.IOUtils.toString(reader);
            String type = message.getClass().getName();

            System.out.println(type);
            System.out.println("Final : " + message);
        }
        reader.close();

        // print result
        System.out.println(response.toString());
        httpClient.close();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return message;
  }

1 个答案:

答案 0 :(得分:0)

因为您在循环之外使用本地“message”变量。添加全局“消息”变量,这应该可以解决您的问题。