1

在这里,我试图从 URL 获取 Json 数据。但是 oit 只显示部分数据。以下是我如何读取数据的详细信息

BufferedInputStream 是;

HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Accept", "application/json");
HttpResponse httpResponse = getHttpClient().execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = new BufferedInputStream(httpEntity.getContent()) ;

public void getJsonwithByteArray(BufferedInputStream istream) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int ctr;
    try {
        ctr = istream.read();
        while (ctr != -1) {
            byteArrayOutputStream.write(ctr);
            ctr = istream.read();
        }
        istream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.v("Text Data", byteArrayOutputStream.toString());
    try {

        // Parse the data into jsonobject to get original data in form of
        // json.
        JSONObject jObject = new JSONObject(
                byteArrayOutputStream.toString());
        jObj = jObject;

        Log.v("JsonParser", "JsonByteArry data: " + jObj.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
4

2 回答 2

2

试试下面的方法:

 /*
 * Call the Webservice read the Json response and return the response in
 * string.
 */
public static String parseJSON(String p_url) {
    JSONObject jsonObject = null;
    String json = null;
    try {
        // Create a new HTTP Client
        DefaultHttpClient defaultClient = new DefaultHttpClient();
        // Setup the get request
        HttpGet httpGetRequest = new HttpGet(p_url);
        System.out.println("Request URL--->" + p_url);
        // Execute the request in the client
        HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
        // Grab the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpResponse.getEntity().getContent(), "UTF-8"));
        json = reader.readLine();
        System.err.println("JSON Response--->" + json);
        // Instantiate a JSON object from the request response
        jsonObject = new JSONObject(json);
    } catch (Exception e) {
        // In your production code handle any errors and catch the
        // individual exceptions
        e.printStackTrace();
    }
    return json;
}

使用上述方法解析 json 响应如下:

   String abcd = CommonUtils.parseJSON(url);
JSONObject jo = new JSONObject(abcd);
于 2013-09-04T07:43:26.347 回答
1

试试这个方法来读取响应

public String getResponseBody(final HttpEntity entity) throws IOException, ParseException {


        if (entity == null) {
            throw new IllegalArgumentException("HTTP entity may not be null");
        }

        InputStream instream = entity.getContent();

        if (instream == null) {
            return "";
        }

        if (entity.getContentLength() > Integer.MAX_VALUE) {
            throw new IllegalArgumentException(

            "HTTP entity too large to be buffered in memory");
        }


        StringBuilder buffer = new StringBuilder();

        BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

        } finally {
            instream.close();
            reader.close();
        }

        return buffer.toString();

    }

如何使用?

HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Accept", "application/json");
HttpResponse httpResponse = getHttpClient().execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String response = getResponseBody(httpEntity);
try {

        // Parse the data into jsonobject to get original data in form of
        // json.
        JSONObject jObject = new JSONObject(
                response);
        jObj = jObject;

        Log.v("JsonParser", "JsonByteArry data: " + jObj.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
于 2013-09-04T07:22:01.150 回答