如何从雅虎天气中检索RSS FEED

时间:2013-07-15 09:08:44

标签: android eclipse rss

我试图从Yahho天气中检索RSS数据,但是我的代码无法正常工作。 我可以知道它有什么问题吗? 我使用的网址是http://weather.yahooapis.com/forecastrss?w=1062617&u=c。 我是编程的新手,请寻求你的建议。

由于

以下是我的编码。

CXActivity.java

public class CXActivity extends Activity {
    /** Called when the activity is first created. */

    ListView listview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RestClient rc = new RestClient();

        //the account key might not work. Please insert your own set.
        ArrayList<Weather> list1 = rc.getPlaces("", "");
        int index = 3;
        String[] listArray = new String[list1.size()];
        StringBuilder sb = new StringBuilder();
        int counter = 0;
        for(int i = 1; i<list1.size(); i++)
        {
                sb.append("\n\nEntry #: " + i);
                sb.append("\n" + "Title: \""+ list1.get(i).getTitle() + "\"");
                sb.append("\n" + "Publish Date: \""+ list1.get(i).getPubDate() +"\"");
                sb.append("\n" + "Condition: \""+ list1.get(i).getCondition()+"\"");
                sb.append("\n" + "Forecast: \""+ list1.get(i).getForecast()+"\"");

                listArray[counter] = sb.toString();
                counter++;
                sb.delete(0, sb.capacity());
        }

        listview=(ListView)findViewById(R.id.ListView01);
        listview.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , listArray));

    }
}

RestClient.java

public class RestClient 
{

    ArrayList weatherList;

    //This is the function responsible to pull data from web service.
    public ArrayList<Weather> getPlaces(String accountKey, String uniqueId) 
    {   
          weatherList = new ArrayList<Weather>();
          try {
            URL _url = new URL("http://weather.yahooapis.com/forecastrss?w=1062617&u=c");
            URLConnection _urlConn = _url.openConnection();

            _urlConn.addRequestProperty("AccountKey", accountKey);
            _urlConn.addRequestProperty("UniqueUserID", uniqueId);

            BufferedReader br = new BufferedReader(new InputStreamReader(_urlConn.getInputStream()));

            String line = null;
            StringBuilder strBuilder = new StringBuilder();

            while ((line = br.readLine()) != null) {
              strBuilder.append(line);
              System.out.println(line);
            }

                String[] IProperties = strBuilder.toString().split("<m:properties>");

                for (String str : IProperties) 
                {
                      Weather weather = new Weather();                    
                      weather.setTitle(Utils.getStringBetween(str, "<title>", "</<title>>"));
                      weather.setPubDate(Utils.getStringBetween(str, "<pubDate>", "</pubDate>"));
                      weather.setCondition(Utils.getStringBetween(str, "<yweather:condition>", "</yweather:condition>"));
                      weather.setForecast(Utils.getStringBetween(str, "<yweather:forecast>", "</yweather:forecast>"));

                      weatherList.add(weather);
                }
            } 
            catch (MalformedURLException ex) 
            {
                ex.printStackTrace();
            } 
            catch (IOException ex) 
            {
                ex.printStackTrace();
            } 
            catch (Exception ex) 
            {
                ex.printStackTrace();
            }

            return weatherList;
        }


    //This is a helper function to get specific traffic data structure
    public Weather getPlacesAtIndex(ArrayList<Weather> list, int index)
    {       
        if(list.size() <= index) return null;

        return list.get(index);
    }

}

Utils.java

public class Utils {

    //This functions get the xml data between the xml elements
    public static String getStringBetween(String src, String start, String end)
    {
        StringBuilder sb = new StringBuilder();
        int startIdx = src.indexOf(start) + start.length();
        int endIdx = src.indexOf(end);
            while(startIdx < endIdx)
            {
                sb.append("" + String.valueOf(src.charAt(startIdx)));
                startIdx++;
            }

        return sb.toString();
    }
}

weather.java

public class Weather {
    String Title;
    String PubDate;
    String Condition;
    String Forecast;



    public String getTitle() {
        return Title;
    }
    public void setTitle(String title) {
        Title = title;
    }
    public String getPubDate() {
        return PubDate;
    }
    public void setPubDate(String pubDate) {
        PubDate = pubDate;
    }
    public String getCondition() {
        return Condition;
    }
    public void setCondition(String condition) {
        Condition = condition;
    }

    public String getForecast() {
        return Forecast;
    }
    public void setForecast(String forecast) {
        Forecast = forecast;
}

}

1 个答案:

答案 0 :(得分:0)

请更改您的活动代码。

公共类MainActivity扩展了Activity

{     /** 在第一次创建活动时调用。 * /

ListView listview;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RestClient rc = new RestClient();

    // the account key might not work. Please insert your own set.
    ArrayList<Weather> list1 = rc.getPlaces("", "");
    int index = 3;
    String[] listArray = new String[list1.size()];
    StringBuilder sb = new StringBuilder();
    int counter = 0;
    Log.e("N", "listArray::" + list1.size());

    for (int i = 0; i < list1.size(); i++) {
        sb.append("\n\nEntry #: " + i);
        sb.append("\n" + "Title: \"" + list1.get(i).getTitle() + "\"");
        sb.append("\n" + "Publish Date: \"" + list1.get(i).getPubDate()
                + "\"");
        sb.append("\n" + "Condition: \"" + list1.get(i).getCondition()
                + "\"");
        sb.append("\n" + "Forecast: \"" + list1.get(i).getForecast() + "\"");

        listArray[counter] = sb.toString();
        counter++;
        sb.delete(0, sb.capacity());
    }
    Log.e("N", "listArray::" + listArray.length);

    listview = (ListView) findViewById(R.id.listView);
    listview.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, listArray));

}

}

相关问题