我有一个方法(下)从我的android程序中的Web服务获取数据。它适用于Android 2.2。但在4.x中,它会抛出一个异常,并显示消息“Error Requesting API”。不知道哪个部分导致我的代码中的异常,为什么只有在4.x有人可以给我一些指针?Android的HttpCleint工作在2.2但不是在4
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:");
}
}
如果你知道造成异常的细节这将有助于..你应该添加到您的ApiException原因(底层除外),这样就可以追查问题。 –
感谢阿卡什和马特的回答/评论。在akash的建议之后,我尝试了一些注意事项,如果我在清单中使targetSdkVersion =“8”,该应用程序在版本3和更高版本中运行。 (NetworkOnMainThreadException在这种情况下,我不承担)。我正在努力使它成为一个异步任务。完成后,应发布我的工作代码(与异步任务)。 – zolio