android - User Input to Url using Async Task -


i'm using xmlpullparser parse page train station api. i'm doing using async task keep newer versions of android.

currently have hardcoded xml string class , results display out in listview.

however, having trouble appending baseurl add user inputted query end of it. had no trouble doing before using async tasks using code along these lines:

    public void stationdetails(){     //--- search button ---     button btnsearch = (button) findviewbyid(r.id.btnsearch);     btnsearch.setonclicklistener(new view.onclicklistener() {      @override     public void onclick(view v) {      //--- edittext view ---     edittext input = (edittext)  findviewbyid(r.id.inputstation);     stringbuilder url = new stringbuilder(baseurl);     url.append(input);     string fullurl = url.tostring();           }       });        } 

but cannot relate async method. can't seem find online , appreciate anyones in matter.

here class hardcodes string:

public class realtime extends activity {  // irish rail site url private static final string baseurl =  "http://api.irishrail.ie/realtime/realtime.asmx/getstationdatabynamexml?stationdesc=malahide"; // xml tag name private static final string tag_item = "objstationdata"; private static final string tag_origin = "origin"; private static final string tag_dest = "destination"; private static final string tag_scharr = "scharrival"; private static final string tag_exparr = "exparrival"; private static final string tag_direct = "direction"; private static final string tag_stat = "status"; private static final string tag_traintype = "traintype";  private realtimelistviewadapter madapter;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.stations_realtime_layout);      listview listview = (listview) findviewbyid(r.id.listview);     madapter = new realtimelistviewadapter(this);     // set adapter     listview.setadapter(madapter);     // use asynctask parse url data     parsetask task = new parsetask(this);     task.execute(baseurl);      // --- register list view long press menu options     registerforcontextmenu(listview); }  @override public boolean oncreateoptionsmenu(menu menu) {     getmenuinflater().inflate(r.menu.main, menu);     return true; }  private class parsetask extends         asynctask<string, void, arraylist<stationdetails>> {     private progressdialog dialog;      public parsetask(context c) {         dialog = new progressdialog(c);     }      @override     protected void onpreexecute() {         dialog.setmessage("loading station info...");         dialog.show();     }      @override     protected arraylist<stationdetails> doinbackground(string... params) {         string strurl = params[0];         httpurlconnection httpconnection = null;         inputstream = null;         try {             url url = new url(strurl);             httpconnection = (httpurlconnection) url.openconnection();             httpconnection.setrequestmethod("get");             httpconnection.setconnecttimeout(10000);             httpconnection.setreadtimeout(10000);             httpconnection.connect();             int responsecode = httpconnection.getresponsecode();             if (responsecode == httpurlconnection.http_ok) {                 = httpconnection.getinputstream();                 return parsenews(is);             }          } catch (exception e) {             // todo         } {             if (is != null) {                 try {                     is.close();                 } catch (ioexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }             }             if (httpconnection != null) {                 httpconnection.disconnect();                 httpconnection = null;             }         }         return null;     }      @override     protected void onpostexecute(arraylist<stationdetails> result) {         // set result         madapter.setdata(result);         // notify refresh         madapter.notifydatasetchanged();          // close progress dialog         if (dialog.isshowing()) {             dialog.dismiss();         }     } }  private arraylist<stationdetails> parsenews(inputstream in)         throws xmlpullparserexception, ioexception {     arraylist<stationdetails> newslist = new arraylist<stationdetails>();     xmlpullparserfactory factory = xmlpullparserfactory.newinstance();     xmlpullparser pullparser = factory.newpullparser();     pullparser.setinput(in, "utf-8");     int eventtype = pullparser.geteventtype();     stationdetails item = null;     while (eventtype != xmlpullparser.end_document) {         string tagname;          if (eventtype == xmlpullparser.start_tag) {             tagname = pullparser.getname();             if (tagname.equals(tag_item)) {                 item = new stationdetails();             } else if (tagname.equals(tag_origin)) {                 if (item != null) {                     item.morigin = pullparser.nexttext();                 }              } else if (tagname.equals(tag_dest)) {                 if (item != null) {                     item.mdestination = pullparser.nexttext();                 }             } else if (tagname.equals(tag_scharr)) {                 if (item != null) {                     item.mscharrival = pullparser.nexttext();                 }             } else if (tagname.equals(tag_exparr)) {                 if (item != null) {                     item.mexparrival = pullparser.nexttext();                 }             } else if (tagname.equals(tag_direct)) {                 if (item != null) {                     item.mdirection = pullparser.nexttext();                 }             } else if (tagname.equals(tag_stat)) {                 if (item != null) {                     item.mstatus = pullparser.nexttext();                 }             }          } else if (eventtype == xmlpullparser.end_tag) {             tagname = pullparser.getname();             if (tagname.equals(tag_item)) {                 newslist.add(item);                 item = null;              }         }         eventtype = pullparser.next();     }     return newslist; } 

edit update

ok put stringbuilder fullurl in onclicklistener button. want execute task when button clicked. moved parsetask task, .excute etc clicklistener. gives me error saying view.onclicklistener realtime undefined, follow quick fixes, when running porject error in logcat saying cannot cast android.content.context.

heres snippet of code looks after quick fix

searchbtn.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view v) {             // append user input baseurl             stringbuilder url = new stringbuilder(baseurl);             url.append(userinput);             string fullurl = url.tostring();              // use asynctask parse url data             parsetask task = new parsetask(this);             task.execute(fullurl);         }     });  public parsetask(onclicklistener onclicklistener) {         dialog = new progressdialog((context) onclicklistener);     } 

and logcat:

enter image description here

i still can't figure out , anybodys welcom

why won't send full url asynctask? like:

stringbuilder url = new stringbuilder(baseurl); url.append(input); string fullurl = url.tostring();// use asynctask parse url data parsetask task = new parsetask(this); task.execute(fullurl); 

Comments