XML feed类的最终结果是错误的

时间:2014-08-11 18:50:44

标签: android eclipse xml-parsing rss

所以即时尝试为汽油价格创建一个XML Feed程序,但是我相信在类中会出现som错误,而不是显示正确的结果,这是我选择的URL XML feed我收到错误消息提示某事出了问题。这是我的班级

package org.me.myandroidstuff;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

//import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class PetrolPriceActivity extends Menu 
{
    private TextView response;
    private TextView errorText;
    private String result;
    private String petrolPriceURL;
    private static final String TAG = "PetrolPrice";
    /** Called when the activity is first created. */
    @Override

    public void onCreate(Bundle savedInstanceState) 
    {

        response = (TextView)findViewById(R.id.title);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);

        Bundle extras = getIntent().getExtras();
        if(extras!=null){
        petrolPriceURL =extras.getString("URLString");
        }
        Log.v(TAG, "index=" + petrolPriceURL);

        // Get the TextView object on which to display the results


        try
        {
            // Get the data from the RSS stream as a string
            result =  petrolPriceString(petrolPriceURL);

            // Do some processing of the data to get the individual parts of the RSS stream
            // At some point put this processing into a separate thread of execution
            // Display the string in the TextView object just to demonstrate this capability
            // This will need to be removed at some point
            response.setText(result);
        }
        catch(Exception ae)
        {
             //Handle error
            response = (TextView)findViewById(R.id.title);
            response.setText("Error");
             //Add error info to log for diagnostics
        } 

    }

    // End of onCreate

    // Method to handle the reading of the data from the RSS stream
    private static String petrolPriceString(String urlString)throws IOException
    {
        String result = "";
        InputStream anInStream = null;
        int response = -1;
        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        // Check that the connection can be opened
        if (!(conn instanceof HttpURLConnection))
                throw new IOException("Not an HTTP connection");
        try
        {
            // Open connection
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            response = httpConn.getResponseCode();
            // Check that connection is Ok
            if (response == HttpURLConnection.HTTP_OK)
            {
                // Connection is OK so open a reader 
                anInStream = httpConn.getInputStream();
                InputStreamReader in= new InputStreamReader(anInStream);
                BufferedReader bin= new BufferedReader(in);

                // Read in the data from the RSS stream
                String line = new String();
                while (( (line = bin.readLine())) != null)
                {
                    result = result + "\n" + line;
                }
            }
        }
        catch (Exception ex)
        {
                throw new IOException("Error connecting");
        }

        // Return result as a string for further processing
        return result;
    }
    // End of petrolPriceString
 // End of Activity class
}

我的日志在我的logcat中显示我已经从我的其他类areaURL成功传递了正确的URL但是当我运行应用程序时它不正确。任何帮助将不胜感激,因为我无法找出问题所在。感谢

这是我的logcat,显示网址是否正确

08-11 14:47:07.750: D/dalvikvm(889): GC_FOR_ALLOC freed 51K, 5% free 2943K/3076K, paused 37ms, total 39ms
08-11 14:47:07.760: I/dalvikvm-heap(889): Grow heap (frag case) to 3.553MB for 635812-byte allocation
08-11 14:47:07.950: D/dalvikvm(889): GC_FOR_ALLOC freed 0K, 4% free 3564K/3700K, paused 33ms, total 33ms
08-11 14:47:08.400: D/dalvikvm(889): GC_FOR_ALLOC freed <1K, 4% free 3569K/3700K, paused 25ms, total 26ms
08-11 14:47:08.430: I/dalvikvm-heap(889): Grow heap (frag case) to 7.220MB for 3840016-byte allocation
08-11 14:47:08.870: D/dalvikvm(889): GC_FOR_ALLOC freed 2K, 2% free 7316K/7452K, paused 187ms, total 187ms
08-11 14:47:09.430: I/Choreographer(889): Skipped 58 frames!  The application may be doing too much work on its main thread.
08-11 14:47:09.450: D/gralloc_goldfish(889): Emulator without GPU emulation detected.
08-11 14:47:11.690: I/Choreographer(889): Skipped 163 frames!  The application may be doing too much work on its main thread.
08-11 14:48:04.135: I/Choreographer(889): Skipped 43 frames!  The application may be doing too much work on its main thread.
08-11 14:48:16.055: I/Choreographer(889): Skipped 47 frames!  The application may be doing too much work on its main thread.
08-11 14:48:17.525: V/AreaURL(889): index=http://www.petrolprices.com/feeds/averages.xml?search_type=town&search_value=Glasgow
08-11 14:48:17.755: I/Choreographer(889): Skipped 67 frames!  The application may be doing too much work on its main thread.
08-11 14:48:18.615: V/PetrolPrice(889): index=http://www.petrolprices.com/feeds/averages.xml?search_type=town&search_value=Glasgow
08-11 14:48:20.215: I/Choreographer(889): Skipped 146 frames!  The application may be doing too much work on its main thread.

1 个答案:

答案 0 :(得分:0)

您正在UI线程上下载Feed。那样不行。使用线程或asynctask进行网络操作。

    MvAsyncDownload dl = 
         new MvAsyncDownload(
              "http://www.example.com/rss.xml", 
              "/mnt/sdcard/rss.xml") { 
        Override 
        protected void onPostExecute(MvException result) { 
            if (result.mbSuccess) { 
              MvMessages.showMessage(this, "Downloaded"); 
            } else { 
              MvMessages.showMessage(this, "Failed " + result.msProblem );
            } 
            super.onPostExecute(result); 
        } 
    }; 

http://www.vsubhash.com/dls/AndroidWithoutStupid/doc/com/vsubhash/droid/androidwithoutstupid/MvAsyncDownload.html