没有从Json获得完整的数据

时间:2013-09-04 07:16:12

标签: android json

这里我试图从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();
    }
}

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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();
    }