为什么我的程序在getResponseCode()上失败?

时间:2013-07-22 16:54:16

标签: java android

我正在尝试做这个教程:http://www.newthinktank.com/2013/05/android-development-tutorial-8/

出于某种原因,它似乎在int responseCode = httpConnection.getResponseCode();

上失败了

所有以前的调试消息都在工作。我真的不明白如何使用logcat错误,所以如果你需要更多的logcat信息,请告诉我要找什么,我会回复它。

private class MyAsyncTask extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... args) {
        try{
            Log.d(TAG, "Start doInBackground");
            URL url = new URL(args[0]);
            Log.d(TAG, "URL");
            URLConnection connection;

            connection = url.openConnection();
            Log.d(TAG, "Open connection");
             HttpURLConnection httpConnection = (HttpURLConnection)connection;
             Log.d(TAG, "httpConnection");

            Log.d(TAG, ""+ httpConnection.getResponseCode());
            int responseCode = httpConnection.getResponseCode();

            Log.d(TAG, "Response code: " + responseCode);
            if(responseCode == HttpURLConnection.HTTP_OK) {
                Log.d(TAG, "Start if");
                InputStream in =  httpConnection.getInputStream();

                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

                DocumentBuilder db = dbf.newDocumentBuilder();

                Document dom = db.parse(in);

                Element docEle = dom.getDocumentElement();


                NodeList nl = docEle.getElementsByTagName("quote");

                if(nl !=  null && nl.getLength() > 0) {

                    Log.d(TAG, "Start second if");
                    for(int i = 0; i<nl.getLength(); i++) {
                    StockInfo theStock = getStockInformation(docEle);
                    daysLow = theStock.getDaysLow();
                    daysHigh = theStock.getDaysHigh();
                    yearLow= theStock.getYearLow();
                    yearHigh = theStock.getYearHigh();
                    name = theStock.getName();
                    lastTradePriceOnly = theStock.getLastTradePriceOnly();
                    change = theStock.getChange();
                    daysRange = theStock.getDaysRange();

                    Log.d(TAG, "End doInBackground");
                    }
                } 
            }

        }

          catch(MalformedURLException e) {
            Log.d(TAG, "MalformedURLException", e);
        } catch(IOException e) {
            Log.d(TAG, "IOException", e);
        } catch(ParserConfigurationException e) {
            Log.d(TAG, "Parser Configuration Exception", e);
        } catch (SAXException e) {
            Log.d(TAG, "SAX Exception", e);
        }
        finally{ }
        return null;
    }

0 个答案:

没有答案