0

我试图从 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);
    }

}

实用程序.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();
    }
}

天气.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;
}

}
4

1 回答 1

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));

}

}

于 2013-07-15T12:50:29.653 回答