I'm developing a weather android app that reads an XML file from the web, and displays the current temperature. In My Async I also get the date like (Thursday 15). When I put this date code in, it makes other values that come back from the XML disrupted. For example:
"Broken Clouds" would appear as "Brok" What's weird is that it only does this like 1 time out of 10.
Here's the code:
public class MyAsyncTask extends AsyncTask < Void, Void, String > {@
Override
protected String doInBackground(Void...params) {
try {
//=============================== Setting Date =====================================
/*DateFormat df = new SimpleDateFormat("EEEE dd");
TextView date = (TextView)findViewById(R.id.date);
date.setText(df.format(Calendar.getInstance().getTime()));*/
//=============================== Getting Data =====================================
URL xmlUrl = new URL("http://ift.tt/1CloN4c");
InputStream in = xmlUrl.openStream();
Document doc = parse( in );
doc.getDocumentElement().normalize();
//================================ Getting Temperature =============================
NodeList nList = doc.getElementsByTagName("temperature");
Node nNode = nList.item(0);
Element eElement = (Element) nNode;
double d = Math.round(Double.parseDouble(eElement.getAttribute("value")));
int dx = (int) d;
xtemp = Integer.toString(dx) + "°";
//================================ Getting Clouds ==================================
NodeList nListw = doc.getElementsByTagName("weather");
Node nNodew = nListw.item(0);
Element eElementw = (Element) nNodew;
xweather = eElementw.getAttribute("value");
//================================ Getting City ====================================
NodeList nListc = doc.getElementsByTagName("city");
Node nNodec = nListc.item(0);
Element eElementc = (Element) nNodec;
xcity = eElementc.getAttribute("name");
} catch (SocketException s) {
internet = false;
} catch (IOException i) {
System.out.println("IO Exception error!");
} catch (Exception ex) {
ex.printStackTrace();
}
return xtemp;
}
No comments:
Post a Comment