0

Yahho weather から RSS データを取得しようとしましたが、コードが機能しませんでした。何が悪いのかわかりますか?私が使用した URL は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();
    }
}

天気.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

アクティビティ コードを変更してください。

public class 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 に答える