将输入流转换为json以显示

时间:2014-02-13 12:13:46

标签: android json

我希望从我的页面接收的数据作为输入流转换为json,而不是在转换时的字符串。 任何人都可以编辑我的代码,以便我可以打动我的PL。我真的需要完成它。

The Code:

     // 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!";

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

    // 11. return result
    return result ;
}

 private static 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;

}   

1 个答案:

答案 0 :(得分:0)

private static JSONObject convertInputStreamToJSONObject(InputStream inputStream)
        throws JSONException {
    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return new JSONObject(result); }

那就行了。