0

我正在尝试做这个教程: 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;
    }
4

0 回答 0