【问题标题】:Not getting complete data from Json未从 Json 获取完整数据
【发布时间】:2013-09-04 07:16:12
【问题描述】:

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

【问题讨论】:

标签: android json


【解决方案1】:

试试下面的方法:

 /*
 * 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);

【讨论】:

    【解决方案2】:

    试试这个方法来阅读回复

    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();
        }
    

    【讨论】:

    • 感谢大家的回复,尤其是 Biraj 的详细解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 2019-12-23
    • 1970-01-01
    • 2021-05-17
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多