1

我已经使用了下面的代码HttpPost,它执行成功。但看不到回复。我明白了InputStream。我想知道里面的内容是什么。

我想显示为字符串格式。如何做到这一点。

        HttpPost httppost = new HttpPost(requestURL);
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.length);

        for (int postValueCount = 0; postValueCount < params.length; postValueCount++)
            nameValuePairs.add(new BasicNameValuePair(params[postValueCount][0],params[postValueCount][1]));

        try {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            Log.d("HttpPostRequest","URL : " + httppost.getRequestLine().getUri());
            Log.d("HttpPostRequest","Method : " + httppost.getRequestLine().getMethod());
            Log.d("HttpPostRequest","Parameters : " + nameValuePairs);
            response = httpclient.execute(httppost);
            status = response.getStatusLine();

            if (status.getReasonPhrase().equals("OK")) {
                httpEntity=response.getEntity();
                InputStream in = httpEntity.getContent();               
                return in;
            } else {
                Log.d("Http Status", status.getReasonPhrase());
            }

        }
4

2 回答 2

1

这是我在我的一个项目中使用的。

static String isToString(InputStream is) {
    StringBuilder sb = null;
    String result = null;
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");
        String line = "0";

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        is.close();
        result = sb.toString();

    } catch (Exception e) {
        System.err.println("Error converting result " + e.toString());
        return null;
    }

    return result;
}
于 2012-08-20T06:24:36.507 回答
1

使用此代码:

HttpEntity entity = response.getEntity();
String response = EntityUtils.toString(entity);
于 2012-08-20T06:28:35.617 回答