我有一种方法(如下)可以从我的 android 程序中的 Web 服务获取数据。它适用于 Android 2.2。但在 4.x 中,它会引发异常,并显示消息“Error Requesting API”。不确定哪个部分导致我的代码中的异常以及为什么仅在 4.x 中有人可以给我一些指示吗?
public static synchronized String performHTTPRequest(HttpUriRequest request)
throws ApiException, ParseException, IOException {
HttpClientWrapper cli=new HttpClientWrapper();
HttpClient client;
try {
client=cli.getClient();
HttpResponse response = client.execute(request);
// Check if server response is valid
StatusLine status = response.getStatusLine() ;
if (status.getStatusCode() != HTTP_STATUS_OK) {
throw new ApiException("Invalid response from server: " + status.toString());
}
return EntityUtils.toString(response.getEntity());
} catch (MalformedURLException e) {
throw new Error(e);
} catch (Exception cnrce) {
throw new ApiException("Error requesting API:");
}
}