Witam ,robię aplikacje pogodową, aktualna pogodę sciągam z http://api.openweathermap.org/ w postaci XML. Korzystam z AsyncTask, problem polega na tym, że gdy chcę wyciągnąc wartość temperatury to nic się nie dzieje, tzn cały czas się wykonuje metoda onPreExecute(). Mam ustawiony internet permission w manifest. Próbowałem na różne sposoby z manipulowaniem START_TAG, END_TAG i nic nie pomaga. Z góry dziękuje za pomoc.
o to kody:
XML:
<current>
<city id="756135" name="Warsaw">
<coord lon="21.01" lat="52.23"/>
<country>PL</country>
<sun rise="2016-08-15T03:20:37" set="2016-08-15T17:58:46"/>
</city>
<temperature value="294.48" min="292.59" max="295.93" unit="kelvin"/>
<humidity value="65" unit="%"/>
<pressure value="1018" unit="hPa"/>
<wind>
<speed value="5.36" name="Gentle Breeze"/>
<gusts/>
<direction value="276.505" code="W" name="West"/>
</wind>
<clouds value="12" name="few clouds"/>
<visibility/>
<precipitation value="24.65" mode="rain" unit="1h"/>
<weather number="503" value="very heavy rain" icon="10d"/>
<lastupdate value="2016-08-15T12:03:56"/>
</current>
Weather.java:
public class Weather extends AsyncTask<String[],Void,String[]> {
private MainActivity activity;
private XmlPullParserFactory factoryObj;
private String url;
private ProgressDialog pDialog;
public Weather(MainActivity activity, String url) {
this.activity = activity;
this.url = url;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(activity);
pDialog.setMessage("Loading...");
pDialog.setTitle("Please wait");
pDialog.show();
}
@Override
protected String[] doInBackground(String[]... params) {
try{
URL myurl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) myurl.openConnection();
connection.setReadTimeout(10000 /* milliseconds */);
connection.setConnectTimeout(15000 /* milliseconds */);
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
InputStream stream = connection.getInputStream();
factoryObj = XmlPullParserFactory.newInstance();
XmlPullParser parser = factoryObj.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,false);
parser.setInput(stream,null);
String[] result = parseXML(parser);
stream.close();
return result;
}catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public String[] parseXML(XmlPullParser parser) throws IOException,XmlPullParserException {
int event;
String text = null;
String[] result = new String[2];
event = parser.getEventType();
while(event != XmlPullParser.END_DOCUMENT){
String name = parser.getName();
switch(event){
case XmlPullParser.END_TAG:
if(name.equals("temperature"))
result[0] = parser.getAttributeValue(null,"value");
break;
}
event = parser.next();
}
return result;
}
@Override
protected void onPostExecute(String[] result) {
super.onPostExecute(result);
pDialog.dismiss();
activity.callBackData(result);
}
}
oraz MainActivity:
public class MainActivity extends Activity {
TextView temp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
temp = (TextView) findViewById(R.id.temptextView);
String url ="http://api.openweathermap.org/data/2.5/weather?q=warsaw&mode=xml&appid=35fae97674d2f8032b66a35addf05450";
new Weather(this,url).execute();
}
public void callBackData(String[] result) {
temp.setText(result[0]);
}
}