Get List from .net Web Service on Android

百般思念 提交于 2019-12-03 14:15:28

问题


I have .net Web Service and I want to use it on android. This web service's methods return List(Of String) and I didn't get response on android. What can I do ? Here is the code..

I'm working on it for 3 days and I didn't find any solution yet.

In short, I need some list from .net web service and add this list's items to spinner. Please help me.

public class MainActivity extends Activity {

    private Spinner myspinner;
    private static final String METHOD_NAME = "Sektorler";  
    private static final String NAMESPACE = "http://tempuri.org/";  
    private static final String SOAP_ACTION = "http://tempuri.org/Sektorler";
    private static final String URL = "http://www.xxxxxxxxxx.com/webservice1.asmx";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myspinner = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.activity_main , SektorDoldur());
        myspinner.setAdapter(adapter);
    }


    private ArrayList<String> SektorDoldur() {

        ArrayList<String> sektorler = new ArrayList<String>();

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;     
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;

        try {

       androidHttpTransport.call(SOAP_ACTION, envelope);
       SoapObject response = (SoapObject) envelope.getResponse();

       if (response.hasProperty("String")) {

       if (response.getPropertyAsString("String") == null) {
    //do something
       } else {

           for(int i=0;i<response.getPropertyCount();i++){
//             sektorler.add(i, response.getPropertyAsString("String"));
               sektorler.add(response.getPropertyAsString(i));
           }
        }

}

        } catch (Exception e) {

            e.printStackTrace();

        }

        return sektorler;

    }

Also I'm trying this code but it doesn't work.

public class MainActivity extends Activity {

    private Spinner myspinner;
    private static final String METHOD_NAME = "Sektorler";  
    private static final String NAMESPACE = "http://tempuri.org/";  
    private static final String SOAP_ACTION = "http://tempuri.org/Sektorler";
    private static final String URL = "http://www.xxxxxxxx.com/webservice1.asmx";
    private String[] denemeList;

    private String[] SektorDoldur(){
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;     
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;

        try {

       androidHttpTransport.call(SOAP_ACTION, envelope);
       SoapObject response = (SoapObject) envelope.bodyIn;
       denemeList = new String[response.getPropertyCount()];

       for(int i=0;i<response.getPropertyCount();i++){ 
           denemeList[i] = response.getPropertyAsString(i).toString();
       }      
} 
        catch (Exception e) {           
            e.printStackTrace();
        }
        return denemeList;      
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SektorDoldur();

        myspinner = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.activity_main , denemeList);
        myspinner.setAdapter(adapter);

    }

回答1:


I got it !!

this is false;

SoapObject response = (SoapObject) envelope.bodyIn;

Lets see this code, it's true;

SoapObject response = (SoapObject) envelope.getResponse();

Finally there are some trick about it.

  1. First of all delete ksoap2 jar file from your project.
  2. Save it and close Eclipse.
  3. Then copy ksoap2 jar file, go to your workspace folder.
  4. Open your project folder, click "libs" folder and paste it.
  5. Open again Eclipse and Run Project!!

        private Spinner myspinner;
        private static final String METHOD_NAME = "Sektorler";  
        private static final String NAMESPACE = "http://tempuri.org";   
        private static final String SOAP_ACTION = "http://tempuri.org/Sektorler";
        private static final String URL = "http://www.xxxxxxxxxx.com/webservice1.asmx";
        private String[] denemeList;
    
        private void SektorDoldur(){
    
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;     
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.debug = true;
    
            try {
    
           androidHttpTransport.call(SOAP_ACTION, envelope);
           SoapObject response = (SoapObject) envelope.getResponse();
           denemeList = new String[response.getPropertyCount()];
    
          for(int i=0;i<response.getPropertyCount();i++){ 
    
               denemeList[i] = response.getPropertyAsString(i).toString();
    
          }      
    } 
            catch (Exception e) {           
                e.printStackTrace();
            }       
        }
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            SektorDoldur();
    
            myspinner = (Spinner) findViewById(R.id.spinner1);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, denemeList);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            myspinner.setAdapter(adapter);
    
        }
    


来源:https://stackoverflow.com/questions/11734702/get-list-from-net-web-service-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!