从Android上的.NET Web服务获取列表(Get List from .net Web Se

2019-07-30 16:51发布

我有.NET Web服务,我想用它在Android上。 此Web服务的方法返回列表(串),我没有得到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);

    }

Answer 1:

我知道了 !!

这是假的;

SoapObject response = (SoapObject) envelope.bodyIn;

让我们来看看这个代码,这是真的;

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

最后,还有一些关于它的伎俩。

  1. 首先,从项目中删除ksoap2 jar文件。
  2. 保存并关闭Eclipse。
  3. 然后复制ksoap2 jar文件,进入工作区文件夹。
  4. 打开您的项目文件夹,单击“库”文件夹中,并将其粘贴。
  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); } 


文章来源: Get List from .net Web Service on Android