How to use REST using Post Method to get XML information in android? -


for example,

uri: http://99.99.99.99:8080/services/api/products/getproduct  method: post  content-type: application/xml 

then, use advanced rest client can these data

<products>   <product>     <category>apple</category>     <productcode>a1</productcode>   </product>   <product>     <category>orange</category>     <productcode>a2</productcode>   </product>   <product>     <category>banana</category>     <productcode>a3</productcode>   </product> .... </products> 

how if want store of category , productcode.

what kinds of method should use?

httppost? xml parser?

this sample code try , nothing logcat.

public void getxml(){                  string result = "";              httpclient client = new defaulthttpclient();              try {                  httppost post = new httppost("http://99.99.99.99:8080/services/api/products/getproduct");                  httpresponse responsepost = client.execute(post);                   httpentity resentity = responsepost.getentity();                  if (resentity != null) {                         result = entityutils.tostring(resentity);                 }              } catch (exception e) {                 e.printstacktrace();             } {                 client.getconnectionmanager().shutdown();             }               system.out.println(result);         } 

i got issue calling api in main thread,

but have call api in background thread

use asyntask

    public class getdetails extends asynctask<string, void, string>{      string response = null;     @override     protected string doinbackground(string... strings) {         try {      httpparams httpparameters = new basichttpparams();     int timeoutconnection = 3000;     httpconnectionparams.setconnectiontimeout(httpparameters, timeoutconnection);     // set default socket timeout (so_timeout)      // in milliseconds timeout waiting data.     int timeoutsocket = 5000;     httpconnectionparams.setsotimeout(httpparameters, timeoutsocket);     defaulthttpclient httpclient = new defaulthttpclient(httpparameters);     httppost httppost = null;      httppost = new httppost("http://99.99.99.99:8080/services/api/products/getproduct");      httppost.setheader("accept", "application/xml");     httppost.setheader("content-type", "application/xml");      httpresponse httpresponse = httpclient.execute(httppost);      inputstream inputstream = httpresponse.getentity().getcontent();     if (inputstream != null)          response = convertinputstreamtostring(inputstream);     else         response = null;      log.v("data list", response);          } catch (exception e) {             log.e("cann", e.tostring());         }          return strings[0];     }      @override     protected void onpostexecute(string result) {     super.onpostexecute(result);            if(response != null){            xmlpullparserfactory factory =xmlpullparserfactory.newinstance();              factory.setnamespaceaware(true);              xmlpullparser xpp = factory.newpullparser();               xpp.setinput(new stringreader (result));              int eventtype = xpp.geteventtype();              while (eventtype != xmlpullparser.end_document) {               if(eventtype == xmlpullparser.start_document) {                   system.out.println("start document");               } else if(eventtype == xmlpullparser.end_document) {                   system.out.println("end document");               } else if(eventtype == xmlpullparser.start_tag) {                   system.out.println("start tag "+xpp.getname());               } else if(eventtype == xmlpullparser.end_tag) {                   system.out.println("end tag "+xpp.getname());               } else if(eventtype == xmlpullparser.text) {              system.out.println("end tag "+xpp.getname());                      }               eventtype = xpp.next();              }          }       }  private string convertinputstreamtostring(inputstream inputstream)         throws ioexception {     bufferedreader bufferedreader = new bufferedreader(             new inputstreamreader(inputstream));     string line = "";     string result = "";     while ((line = bufferedreader.readline()) != null)         result += line;      inputstream.close();     return result;  }     }  

and last calling getxml() method change new getdetails().execute("")


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -