我正在构建一个应用程序,其中我的一个模块包含天气预报。我正在使用SAX Parser,到目前为止,我正在解析XML文件并获得单个地方的结果,并且在这里:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.saxmain);
ScrollView MyScrollView = new ScrollView(this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
MyScrollView.addView(layout);
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL(
"http://192.168.1.31:81/Udayavani/android/weather/weather.php?city=mumbai");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/* Get result from MyXMLHandler TagList Object */
taglist = MyXMLHandler.sitesList;
for (int i = 0; i <26; i++) {
texts[i] = new TextView(this);
texts[i].setText(""+i);
texts[i].setTypeface(kannada);
layout.addView(texts[i]);
}
/** Set the result text in textview and add it to layout */
texts[0].setText("Current= "+taglist.getCurrent().get(0));
texts[1].setText("Temp= "+taglist.getTemp().get(0));
texts[2].setText("Humidity= "+taglist.getHumidity().get(0));
texts[3].setText(""+taglist.getWind_condition().get(0));
texts[4].setText(""+taglist.getIcon().get(0));
int j=0;
int k=5;
while(k<20){
texts[k].setText("----------------------------------");
k++;
texts[k].setText("Day= "+taglist.getDay().get(j));
k++;
texts[k].setText("Condition= "+taglist.getCondition().get(j));
k++;
texts[k].setText("Low= "+taglist.getLow().get(j));
k++;
texts[k].setText("High= "+taglist.getHigh().get(j));
k++;
texts[k].setText("Icon= "+taglist.getIcon().get(j));
k++;
j++;
if(j>=5){
j=0;
}
}
/** Set the layout view to display */
setContentView(MyScrollView);
}
}
我的问题是,有人可以帮助我获取各个城市的解析Xml文件吗?我有几乎所有城市的URL,我可以通过更改我在我的代码中提到的URL(比如weather.php?city = auckland)中的城市名称来获得它。我开始使用ListView和我有大约20-30个城市存储在ListView中作为列表内容。每当我点击特定的地方,我想得到该特定城市的解析XMl文件。
任何人都可以指导我。
由于
xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<weather>
<current_conditions>
<current>ಹೊಗೆಯಾವೃತ</current>
<temp>95</temp>
<humidity>Humidity: ೨೩%</humidity>
<wind_condition>Wind: ಪೂ at 13 km/h</wind_condition>
<icon>smoke.gif</icon>
</current_conditions>
<forecast_information>
<forecast0>
<day>ಶನಿ.</day>
<condition>ಬಹುಪಾಲು ಬಿಸಿಲು</condition>
<low>25</low>
<high>36</high>
<icon>mostly_sunny.gif</icon>
</forecast0>
<forecast1>
<day>ರ.</day>
<condition>ಶುಭ್ರ</condition>
<low>25</low>
<high>35</high>
<icon>sunny.gif</icon>
</forecast1>
<forecast2>
<day>ಸೋ.</day>
<condition>ಬಹುಪಾಲು ಬಿಸಿಲು</condition>
<low>25</low>
<high>36</high>
<icon>mostly_sunny.gif</icon>
</forecast2>
<forecast3>
<day>ಮಂ.</day>
<condition>ಬಹುಪಾಲು ಬಿಸಿಲು</condition>
<low>26</low>
<high>33</high>
<icon>mostly_sunny.gif</icon>
</forecast3>
</forecast_information>
答案 0 :(得分:2)
我得到了解决方案。我正在声明一个名为StateNames []的数组。从我的ListView类中,我将putExtra(“id”,pos)传递给我已声明我的数组的类,我正在访问通过以下内容:
String actualUrl="http://192.168.1.31:81/Udayavani/android/weather/weather.php?city=";
private String[] StateNames={..,..,..};
//In OnCreate()
Bundle b = getIntent().getExtras();
id= b.getInt("id");
String myURL=actualUrl+StateNames[id];
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL(myURL);
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
答案 1 :(得分:0)
使用列表视图而不是滚动视图。使用列表视图的onListItemClick()方法,获取特定的列表项(在你的情况下为city)点击并将其作为参数传递给ur函数(应该能够获取传递给url的城市的xml文件)。有关列表视图see this
的更多信息