Http得不到所有数据

时间:2013-11-13 01:22:40

标签: java android http

我使用以下代码从Web API服务接收数据。问题是,并非所有数据都被收到。我已经使用fiddler检查了所有数据是否实际被发送,但程序正在接收大约2%的数据。

    public String getAllTableDataWithBearerToken (String tableName) {

    HttpClient client = new DefaultHttpClient();        
    HttpGet httpGet = new HttpGet(HOST + API_ACCESS_URL + tableName);
    httpGet.addHeader("Accept", "application/json");
    httpGet.addHeader("Authorization", "Bearer " + bearerToken);
    String responseString; 
    try {
        HttpResponse response = client.execute(httpGet);
        if (response.getStatusLine().getStatusCode()!=200) return "Failed to get table data for table: " + tableName + " with error: " + response.getStatusLine().toString();

        HttpEntity entity = response.getEntity();
        if(entity != null) {
            Log.d("INFO", "There was an entity to read");
            InputStream stream = entity.getContent();
            responseString = convertStreamToString(stream);
            stream.close();

        } else { 
            Log.d("INFO", "No entity received");
            return "No data to get for table: "  + tableName;
        }

    } catch (ClientProtocolException e) {
        Log.d("INFO", "Error getting entity data.  Error is: ");
        e.printStackTrace();
        return "ClientProtocolException getting data for table: "  + tableName + " with error: " + e.getLocalizedMessage();
    } catch (IOException e) {
        Log.d("INFO", "Error getting entity data.  Error is: ");
        e.printStackTrace();
        return "IOException getting data for table: "  + tableName + " with error: " + e.getLocalizedMessage();
    }

和输入流转换器功能是:

    private static String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        Log.d("INFO", "Error reading input stream.  Error is: ");
       e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            Log.d("INFO", "Error closing input stream.  Error is: ");
            e.printStackTrace();
        }
    }
    Log.d("INFO", "Returning read data: " + sb.toString());;

    return sb.toString();
}

2 个答案:

答案 0 :(得分:0)

不确定您正在阅读什么类型的字符串,但您可能会达到获取大小限制 maximum length of HTTP GET request?

尝试将其作为帖子发送,看看是否能解决问题。

如果没有,那么它将需要更多的堆栈跟踪和一些打印输出来查看您正在接收的内容。

答案 1 :(得分:0)

抱歉,问题是记录了响应 - 它有一个最大值。当我写出流的最后500个字符时 - 它确实是流的最后500个字符。