DocumentBuilder解析异常

时间:2016-02-13 12:31:41

标签: java android android-asynctask xml-parsing

我是Java&机器人。

我试图在AsyncTask的上下文中解析InputStream并获得异常。 我做错了什么?

 public Integer doInBackground(String... url)
{
    HttpURLConnection con = null;
    int status = ResultStatus.STATUS_OK;
    InputStream in = null;

    ConnectivityManager cm = (ConnectivityManager) mainActivityContext.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (cm.getActiveNetworkInfo() == null)
        return ResultStatus.CONNECTION_FAILED;

    try
    {
        URL bank_url = new URL(url[0]);
        con = (HttpURLConnection) bank_url.openConnection();
        con.getResponseCode();
        con.setRequestMethod("GET");
        con.connect();
        in = con.getInputStream();
    }
    catch (IOException e)
    {
        status = ResultStatus.URL_NOT_FOUND;
    }
    finally
    {
        con.disconnect();
    }

    try
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(in);    <<<<----Here I get an exception
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return status;
}

我得到一个异常,出现在:Document doc = builder.parse(in);

异常输出:

-13 14:11:26.651 13568-14118/com.course.rita.currencyexchange W/System.err: java.io.IOException: closed
02-13 14:11:26.661 13568-13575/com.course.rita.currencyexchange W/art: Suspending all threads took: 7.999ms
02-13 14:11:26.661 13568-14118/com.course.rita.currencyexchange W/System.err:     at com.android.okio.RealBufferedSource$1.read(RealBufferedSource.java:158)
02-13 14:11:26.661 13568-14118/com.course.rita.currencyexchange W/System.err:     at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1642)
02-13 14:11:26.661 13568-14118/com.course.rita.currencyexchange W/System.err:     at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:111)
02-13 14:11:26.661 13568-14118/com.course.rita.currencyexchange W/System.err:     at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:107)
02-13 14:11:26.661 13568-14118/com.course.rita.currencyexchange W/System.err:     at com.course.rita.currencyexchange.ReadCurrencyRateTask.doInBackground(ReadCurrencyRateTask.java:86)
02-13 14:11:26.661 13568-14118/com.course.rita.currencyexchange W/System.err:     at com.course.rita.currencyexchange.ReadCurrencyRateTask.doInBackground(ReadCurrencyRateTask.java:34)
02-13 14:11:26.661 13568-14118/com.course.rita.currencyexchange W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-13 14:11:26.661 13568-14118/com.course.rita.currencyexchange W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-13 14:11:26.661 13568-14118/com.course.rita.currencyexchange W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-13 14:11:26.661 13568-14118/com.course.rita.currencyexchange W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

由于

1 个答案:

答案 0 :(得分:0)

finally块应该在第二个catch块之后。

finally
    {
        con.disconnect();
    }

由于此处将断开连接。