Luigi Cerone Luigi Cerone - 3 months ago 16
Java Question

AsyncTask with HTTP get and fetch of .xml

I was developing an Android app where by clicking on buttons I could control the digital state of an Arduino board. Arduino responses with a XML file created during runtime and, by fetching its contents, I'd like to display the actually state of the pins.

Now, I could realize it in fact my app works but I can't handle the fetchXML() method in an AsyncTask instance. Sometimes my UI freezes and I don't want that.

This is the code of my main activity:

public class MainActivity extends ActionBarActivity {

private String url = "http://192.168.0.254/?domotica";
private EditText button1,button2,button3,button4;
private HandleXML obj;

@Override
protected void onCreate(Bundle savedInstanceState) {

/* if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
*/
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (EditText)findViewById(R.id.editText1);
button2 = (EditText)findViewById(R.id.editText2);
button3 = (EditText)findViewById(R.id.editText3);
button4 = (EditText)findViewById(R.id.editText4);



Button led1on = (Button) findViewById(R.id.led_1on);
Button led1off = (Button) findViewById(R.id.led_1off);

led1on.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
//commandArduino("http://192.168.0.254/?b1o");
Invio inv = new Invio();
inv.execute("http://192.168.0.254/?b1o");

}
});

led1off.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
//commandArduino("http://192.168.0.254/?b1f");
Invio inv = new Invio();
inv.execute("http://192.168.0.254/?b1f");
}
});

}

private class Invio extends AsyncTask<String,Void,String> {

@Override
protected String doInBackground(String... params) {
int count = params.length;

for (int i = 0; i < count; i++) {
try {
HttpClient httpclient = new DefaultHttpClient();
httpclient.execute(new HttpGet(params[i]));
} catch (Exception e) {
e.printStackTrace();

}
}
return null;
}
}

public void open(View view){
String finalUrl = url;
obj = new HandleXML(finalUrl);
obj.fetchXML();
while(obj.parsingComplete);
button1.setText(obj.getbutton1());
button2.setText(obj.getbutton2());
button3.setText(obj.getbutton3());
button4.setText(obj.getbutton4());

}

/*public void commandArduino(String urlz) {


}*/
@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);
}
}


and that's the code of my XML class used to handle and to fetch XML file and contents:

public class HandleXML {

private String button1 = "button1";
private String button2 = "button2";
private String button3 = "button3";
private String button4 = "button4";
private String urlString = null;
private XmlPullParserFactory xmlFactoryObject;

public volatile boolean parsingComplete = true;

public HandleXML(String url){
this.urlString = url;
}
public String getbutton1(){
return button1;
}
public String getbutton2(){
return button2;
}
public String getbutton3(){
return button3;
}
public String getbutton4(){
return button4;
}

public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
String text=null;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name=myParser.getName();
switch (event){
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;

case XmlPullParser.END_TAG:
if(name.equals("button1")){
button1 = text;
}
else if(name.equals("button2")){
button2 = text;// myParser.getText();
}
else if(name.equals("button3")){
button3 = text;//myParser.getText();
}
else if(name.equals("button4")){
button4 = text;//myParser.getText();
}
else{
}
break;
}
event = myParser.next();

}
parsingComplete = false;
} catch (Exception e) {
e.printStackTrace();
}

}
public void fetchXML(){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setReadTimeout(1000 /* milliseconds */);
conn.setConnectTimeout(1500 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
InputStream stream = conn.getInputStream();

xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();

myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES
, false);
myparser.setInput(stream, null);
parseXMLAndStoreIt(myparser);
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});

thread.start();
}
}

Answer

As suggested by @njzk the problem was in this statment:

while(obj.parsingComplete);

That creates an endless loop, I've removed it and now it is working.