gpt4 book ai didi

android - 从 Android 上的 .net Web 服务获取列表

转载 作者:搜寻专家 更新时间:2023-11-01 09:06:13 26 4
gpt4 key购买 nike

我有 .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);

}

最佳答案

我明白了!!

这是错误的;

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

    }

关于android - 从 Android 上的 .net Web 服务获取列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11734702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com