使用Java提取谷歌API的天气数据 [英] Using Java to extract data from google Weather API
问题描述
我一直在使用XML文档和Java玩弄了一下最近,但我已经使用谷歌API的天气绝对没有运气。
I've been playing around with xml documents and java a bit recently, but I've had absolutely no luck using the google weather API.
让我们假设我试图做一个简单的对象来存储当前的温度,和预测的温度只是明天,我会怎么做呢?
Let's assume that I'm trying to do a simple object to store current temp, and forecast temp for just tomorrow, how would I do this?
http://www.google.com/ig/api?weather=02110 是我的家乡工作的例子。
http://www.google.com/ig/api?weather=02110 Is the working example for my home city.
感谢
使用这个code:
public static final String[] xmlLoader(){
String xmlData[] = new String[2];
try {
URL googleWeatherXml = new URL("http://www.google.com/ig/api?weather=02110");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(googleWeatherXml.openStream());
// normalize text representation
doc.getDocumentElement ().normalize ();
NodeList listOfWeek = doc.getElementsByTagName("");
Node firstWeekNode = listOfWeek.item(dateCounter-1);
int totalWeeks = listOfWeek.getLength();
//Break xml file into parts, then break those parts down int an array by passing individual elements to srtings
if(firstWeekNode.getNodeType() == Node.ELEMENT_NODE){
Element firstWeekElement = (Element)firstWeekNode;
//-------
NodeList dateList = firstWeekElement.getElementsByTagName("date");
Element dateElement = (Element)dateList.item(0);
NodeList textDateList = dateElement.getChildNodes();
xmlData[0]= (((Node)textDateList.item(0)).getNodeValue().trim()).toString();
//-------
NodeList riddleList = firstWeekElement.getElementsByTagName("riddle");
Element riddleElement = (Element)riddleList.item(0);
NodeList textRiddleList = riddleElement.getChildNodes();
xmlData[1]= (((Node)textRiddleList.item(0)).getNodeValue().trim()).toString();
//----
NodeList lWSList = firstWeekElement.getElementsByTagName("lastWeekSolution");
Element ageElement = (Element)lWSList.item(0);
NodeList textLWSList = ageElement.getChildNodes();
xmlData[2]= (((Node)textLWSList.item(0)).getNodeValue().trim()).toString();
//------
}//end of if clause
}
catch(MalformedURLException f){System.err.println(f.getMessage()); }
catch(NullPointerException npe){
System.out.println("The Weather Data you searched for is incorrect or does not yet exist, try again. ");
String s[] = {" ", " "};
main(s);
}
catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}
catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}catch (Throwable t) {
t.printStackTrace ();
}
return xmlData;
}
获取万吨空指针,没有不管我做什么。
Getting tons of null pointers, no matter what I do.
推荐答案
当你调用.getElementsByTagName(),你需要把一名警卫在你回来的值:
When you call .getElementsByTagName(), you need to put a guard on the value you get back:
NodeList dateList = firstWeekElement.getElementsByTagName("date");
if (datelist != null) {
Element dateElement = (Element)dateList.item(0);
等等。
更新
现在,我看看XML,我看到有没有<日期和GT;要素;所以这就是为什么dateList为空。你想寻找类似的元素<&forecast_information GT;和<全市方式>
Now that I look at the XML, I see that there are no <date> elements; so that's why dateList is null. You want to be looking for elements like <forecast_information> and <city>.
NodeList forecasts = firstWeekElement.getElementsByTagName("forecast_information");
if (forecasts != null) {
Element forecastElement = (Element)forecasts.item(0);
这篇关于使用Java提取谷歌API的天气数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!