2

我有 .net Web 服务,我想在 android 上使用它。此 Web 服务的方法返回 List(Of String) 并且我在 android 上没有得到响应。我能做些什么 ?这是代码..

我正在研究它 3 天,但我还没有找到任何解决方案。

简而言之,我需要一些来自 .net Web 服务的列表,并将该列表的项目添加到微调器。请帮我。

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;

    }

我也在尝试这段代码,但它不起作用。

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);

    }
4

1 回答 1

3

我知道了 !!

这是错误的;

SoapObject response = (SoapObject) envelope.bodyIn;

让我们看看这段代码,它是真的;

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

最后有一些技巧。

  1. 首先从你的项目中删除 ksoap2 jar 文件。
  2. 保存并关闭 Eclipse。
  3. 然后复制 ksoap2 jar 文件,转到您的工作区文件夹。
  4. 打开您的项目文件夹,单击“libs”文件夹并粘贴它。
  5. 再次打开 Eclipse 并运行项目!!

        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);
    
        }
    
于 2012-08-01T06:10:09.267 回答