0

我正在使用以下代码从 Web API 服务接收数据。问题是,并非所有数据都被接收到。我已经使用提琴手检查了所有数据实际上都在发送,但程序正在接收大约 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();
}
4

2 回答 2

0

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

于 2013-11-13T04:37:14.553 回答
0

不确定您正在阅读哪种字符串,但您可能正在达到 HTTP GET 请求的 Get size limit 最大长度?

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

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

于 2013-11-13T03:52:42.600 回答