Return an Array of Strings from an ASP.NET Web Service to an Android client using SOAP -


i want return array of strings android client , populate listview. using soap library (org.ksoap2.*) invoke asp.net web service.

here code web service:

1. asp web service

    imports ...     imports system.web.services     imports system.web.services.protocols     imports ...      <webservice(namespace:="...")>_     <webservice(conformsto:=...)> _     <global.microsoft.visualbasic.compilerservices.designergenerated()> _      public class enquiryws         inherits system.web.services.webservice      ' web method            <webmethod()> _            public function getlist() string()                   'hardcoded list                   return new string() { "item1", "item2", "item3" }            end function 

i've tested web service accessing asmx, there no runtime errors. i've tested simple string, web service returned string android. this:

    ' web method     <webmethod()> _     public function getstring() string            return "my string."     end function 

2. android activity

secondly, here android code invoking asp.net web service.

    import org.ksoap2.soapenvelope;     import org.ksoap2.serialization.soapobject;     import org.ksoap2.serialization.soapprimitive;     import org.ksoap2.serialization.soapserializationenvelope;     import org.ksoap2.transport.httptransportse;      public class mainactivity extends appcompatactivity {             private arraylist<string> list;            private listview listview;            private arrayadapter<string> adapter;             @override            protected void oncreate(bundle savedinstancestate) {                      //...                      new getpersonlist().execute("asynctask string");                      //...            }             // inner asynctask class            private class getpersonlist extends asynctask<string,  integer,string> {                    private static final string soap_action = "https://mynamespace/getlist";                    private static final string method_name = "getlist";                    private static final string namespace = "https://mynamespace/";                    private static final string url =             "https://myiissite/myasmxfile.asmx";                     @override                    protected void onpreexecute() {                              super.onpreexecute();                              // onpreexecute stuff                    }                     @override                    protected string doinbackground(string... arg) {                              string result = null;                               soapobject request = new soapobject(namespace, method_name);                               //create envelope                              soapserializationenvelope envelope = new soapserializationenvelope(soapenvelope.ver11);                               //required .net                              envelope.dotnet = true;                               //set output soap object                              envelope.setoutputsoapobject(request);                               //create http call object                              httptransportse androidhttptransport = new httptransportse(url);                               try {                                   //invoke web service                                   androidhttptransport.call(soap_action, envelope);                                    //get response                                   soapprimitive response = (soapprimitive) envelope.getresponse();                                   //assign response static variable                                   result = response.tostring();                              } catch (exception e) {                                   result = "error " + e.getmessage();                              }                               return result;                    }                     @override                    protected void onpostexecute(string result) {                              system.out.println("returned soap xml: " + result);                              myfunction(result);                    }            }     } 

myfunction method created additional work.

3. myfunction

here myfunction method code:

    public void myfunction(string s) {            // add webservice response list            list.add(s);             // set adapter            adapter = new arrayadapter<string>(mainactivity.this, android.r.layout.simple_list_item1, list);            listview.setadapter(adapter);      } 

the argument pass myfunction soap response, add list , set adapter.

okay, web service returning array of strings, overriden onpostexecute method working 1 string, if declare onpostexecute parameter collection, not overriding anymore.

this error getting in logcat:

    return soap xml: error expected: start_tag {http://schemas.xmlsoap.org/soap/envelope/}envelope (position:start_tag <html>@1:7 in java.io.inputstreamreader@4182d238) 

could please advise?

i have found solution. casting response soapobject , not soapprimitive, reason because soapprimitive primitive data types, soapobject supports composite data types. therefore, casting array soapobject , not soapprimitive anymore.

i have removed myfunction() method, because setting adapter in onpostexecute() method override run(). , finally, have added progressdialog, showing progressdialog in onpreexecute() method while it's processing, , invoking dismiss() in onpostexecute() method.

    private class getpersonlist extends asynctask<void, void, string> {      private static final string soap_action = "http://mynamespace/mymethod";     private static final string method_name = "mymethod";     private static final string namespace = "http://mynamespace/";     private static final string url =             "http://myurl/myasmxfile.asmx";      progressdialog progressdialog;      @override     protected void onpreexecute() {         super.onpreexecute();         progressdialog= progressdialog.show(mainactivity.this,                 "wait",                 "retrieving data",                 true         );     }      @override     protected string doinbackground(void... params) {         string finalresult = null;          //create request object         soapobject request = new soapobject(namespace, method_name);          //create envelope add our request object         soapserializationenvelope envelope = new soapserializationenvelope(                 soapenvelope.ver11);          //required .net         envelope.dotnet = true;          //add request object envelope         envelope.setoutputsoapobject(request);          //create http call object         httptransportse androidhttptransport = new httptransportse(url);          try {             //invoke web service             androidhttptransport.call(soap_action, envelope);              // response             // cast soapobject , not soapprimitive             soapobject response = (soapobject) envelope.getresponse();              //assign response static variable             finalresult = response.tostring();              // loop through response (loop through properties)             // , add them list             for(int = 0; < response.getpropertycount(); i++) {                 list.add(response.getproperty(i).tostring());             }          } catch (exception e) {             system.out.println("######## error " + e.getmessage());         }          return finalresult;     }      @override     protected void onpostexecute(string str) {         progressdialog.dismiss();         system.out.println("returned soap xml: " + str);          runonuithread(new runnable() {             @override             public void run() {                 // set adapter                 adapter = new arrayadapter<string>(mainactivity.this, r.layout.list_item, r.id.product_name, list);                  listview.setadapter(adapter);             }         });     } } 

i have heard there way of doing using gson/json, post once have figured out.

cheerz


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -