My problem is this:
In my android project I am using an API that provides data on the weather forecast of a city, I retrieve this data in an XML file; my problem is that I can not "separate" the data by label to manipulate the condigo is as follows:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Descarga dc = new Descarga();
dc.execute("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class Descarga extends AsyncTask<String,Integer,String> {
private String xml;
private ProgressDialog dialog;
public Descarga(){
xml = "";
dialog = new ProgressDialog(MainActivity.this);
}
@Override
protected String doInBackground(String... params) {
xml = getXmlFromUrl("http://api.meteored.mx/index.php?api_lang=mx&localidad=22299&affiliate_id=do2kuih35e2g&v=2");
return null;
}
@Override
protected void onPreExecute(){
dialog.setCancelable(false);
dialog.isIndeterminate();
dialog.show();
super.onPreExecute();
}
//En este método mediante el Toast se puede observar los datos si son
//recuperados
@Override
protected void onPostExecute(String resut){
dialog.cancel();
//Toast.makeText(MainActivity.this,xml,Toast.LENGTH_LONG).show();
getData(xml);
super.onPostExecute(resut);
}
private String getXmlFromUrl(String url) {
String sxml ="";
try {
DefaultHttpClient clienteHttp = new DefaultHttpClient();
HttpPost postHttp = new HttpPost(url);
HttpResponse respuesta = clienteHttp.execute(postHttp);
HttpEntity entidad = respuesta.getEntity();
sxml = EntityUtils.toString(entidad,"utf-8");
}catch (UnsupportedEncodingException ex){
}catch (ClientProtocolException ex){
}catch (IOException ex){
}
return sxml;
}
/*Este es el metodo donde quiero recuperar los datos pero no lo consigo
ya verifique que el archivo si lo esta recuperando */
public void getData(String x){
XmlPullParserFactory factory = null;
XmlPullParser parse = null;
String texto = "";
try{
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parse = factory.newPullParser();
parse.setInput(new ByteArrayInputStream(x.getBytes("utf-8")),null);
int evenType = parse.getEventType();
while(evenType != XmlPullParser.END_DOCUMENT){
String tagName = parse.getName();
switch (evenType){
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
texto = parse.getText();
Toast.makeText(MainActivity.this,texto,Toast.LENGTH_LONG).show();
break;
case XmlPullParser.END_TAG:
if(tagName.equalsIgnoreCase("20170921")){
TextView text = (TextView)findViewById(R.id.textday);
text.setText(""+parse.getAttributeValue(null,"value"));
}
break;
default:
break;
}
}
}catch (Exception e){}
}
}
}
The XML file that I am using is the following:
http://api.meteored.mx/index.php?api_lang=mx&localidad=22299&affiliate_id=do2kuih35e2g&v=2
I appreciate who can support me.