我正在使用以下代码从 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();
}