Android:异常后不要退出

时间:2014-03-17 13:03:34

标签: android exception login dialog httpclient

如何让我的应用在UnknownHostException之后不会退出?

我的代码实际上是:

    public String getXmlFromUrl(String url, String Username, String Passwd){
    String xml = null;
    try{
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());

        NTCredentials creds = new NTCredentials(Username, Passwd, "AndroidApp", "****");

        httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
        HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 1000);

        HttpGet httpGet = new HttpGet(url);
        httpGet.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnknownHostException e){
        e.printStackTrace();
        Log.e("HTTPClient", "Unknown Host");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (ConnectTimeoutException e) {
        e.printStackTrace();
        Log.e("HTTPClient", "Connection Timeout");
    } catch (NoHttpResponseException e) {
        e.printStackTrace();
        Log.e("HTTPClient", "No Response");
    } catch (ParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();            
    }

    return xml;
}

要获得异常是单击登录按钮,然后应用程序通过手机上的http加载xml文件。 用户的对话框代替异常,主机名不存在。

与其他例外相同......

更新: 好的,我找到了解决方案。 我在片段

之间切换时添加了NullPointerException

0 个答案:

没有答案