错误:IOException - 如何获得更具体的错误

时间:2012-03-23 01:45:50

标签: java android

如何找出更具体的错误情况,例如错误是由于FileNotFoundException还是EOFExceptionor No Internet Access等等。

下面是我用来访问互联网的代码,我正在获取IOException,我知道这是由于没有访问互联网的权限..但我想知道如何确定是否此错误与文件或eof或权限相关....

下面是我的代码并捕获所有异常,但没有一个会给我一个特定的情况例外。

try { 
    HttpGet request = new HttpGet(MyConstants.URL);
    HttpResponse response = null;
    response = httpClient.execute(request);
    //HttpResponse httpResponse = httpClient.execute(request);
    HttpEntity httpEntity = response.getEntity();
    xmlList = EntityUtils.toString(httpEntity);
} 
catch (MalformedURLException e) {
    xmlList = "URL: is a malformed URL";
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    xmlList = "URL: UnsupportedEncodingException";
} catch (ClientProtocolException e) {
    e.printStackTrace();
    xmlList = "URL: ClientProtocolException"; 
} catch (SocketTimeoutException  e) {
    e.printStackTrace();
    xmlList = "URL: SocketTimeoutException";
} catch (ConnectTimeoutException  e) {
    e.printStackTrace();
    xmlList = "URL: ConnectTimeoutException";
} catch (IOException e) {
    xmlList = "URL: IOException";
    e.printStackTrace();
} 

1 个答案:

答案 0 :(得分:0)

抓住它!

} catch (FileNotFoundException e) {
    xmlList = "URL: FileNotFoundException";
    e.printStackTrace();
} catch (EOFException e) {
    xmlList = "URL: EOFException";
    e.printStackTrace();
} catch (IOException e) {
    xmlList = "URL: IOException";
    e.printStackTrace();
}

没有“无法访问互联网”这样的例外情况。