android - 如何使用 KSOAP2 从 body 获取数据 - android

标签 android web-services asp.net-mvc-4 soap ksoap2

我正在尝试使用 ksoap2 库连接到 SOAP 网络服务。我已经阅读了很多关于它的文档,但最后他向我展示了空数组,请帮助我。

POST /musicappservice.asmx HTTP/1.1
Host: musicappwebservice.dev07.company.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://company.com/wsAppCategory"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <wsAppCategory xmlns="http://company.com/">
      <json>string</json>
    </wsAppCategory>
  </soap:Body>
</soap:Envelope>



HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <wsAppCategoryResponse xmlns="http://company.com/">
      <wsAppCategoryResult>string</wsAppCategoryResult>
    </wsAppCategoryResponse>
  </soap:Body>
</soap:Envelope>

这是我正在使用的网络服务

这里是 Java 代码

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv = (TextView) findViewById(R.id.tv_result);
    b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            AsyncCallWS task = new AsyncCallWS();
            task.execute();
        }
    });
}

public void Sonsgs_category(String songs_cat) {
    //Create request
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    Log.i("i = ",""+request);

    //Property which holds input parameters

    PropertyInfo AppDevId =new PropertyInfo();
    AppDevId.name = "AppDevId";
    AppDevId.setValue("24");
    request.addProperty(AppDevId);
    Log.i("AppDevId = ",""+AppDevId);

    PropertyInfo UserId =new PropertyInfo();
    UserId.name = "UserId";
    UserId.setValue("1");
    request.addProperty(UserId);
    Log.i("UserId = ",""+UserId);

    //Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    //Set output SOAP object
    envelope.setOutputSoapObject(request);
    //Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        jsonarray = response.toString();
        Log.i("jsonarray = ",""+jsonarray);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private class AsyncCallWS extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... params) {
        Log.i(TAG, "doInBackground");
        Sonsgs_category(celcius);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.i(TAG, "onPostExecute");
        tv.setText(jsonarray);
    }

    @Override
    protected void onPreExecute() {
        Log.i(TAG, "onPreExecute");
        tv.setText("Loading...");
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        Log.i(TAG, "onProgressUpdate");
    }

}

最佳答案

正如您评论说要解析此对象并生成 arrayList,我将向您发送创建列表的演示代码。

public void LoadMakeList(SoapObject soapObject) {
    ArrayList<ModelTest> list = new ArrayList<>();
    for (int i = 0; i < soapObject.getPropertyCount(); i++) {
        ModelTest model = new ModelTest();
        model.setAddress(soapObject.getPropertyAsString("your_property_name"));
        model.setName(soapObject.getPropertyAsString("your_property_name_1"));
        list.add(model);
    }
}

和一个演示自定义模型类:

public class ModelTest {

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return Address;
}

public void setAddress(String address) {
    Address = address;
}

String name, Address;

通过这段代码,您可以了解如何从 SoapObject 生成自定义模型的数组列表。

注意:我没有运行此代码,因为我正在使用 magento 并通过引用我创建它的代码来解析该响应。谢谢

List<MyObject> list = new ArrayList<MyObject>();
if (soapObject != null && soapObject.getPropertyCount() > 0) {
   for (int i = 0; i < soapObject.getPropertyCount(); i++) {
      SoapObject so = (SoapObject) soapObject.getProperty(i);
      MyObject obj = new MyObject();
      obj.setProperty1(so.getPropertyAsString("property1"));
      obj.setProperty2(so.getPropertyAsString("property2"));
      list.add(obj);
   }
}

这对我有用。

关于android - 如何使用 KSOAP2 从 body 获取数据 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33390160/

相关文章:

android - 当函数被挂起时会发生什么。暂停期间, "suspend"函数去了哪里

jquery - $.ajax() 在 jquery-1.6.2 中出错,但在 jquery-1.4.1 中没有

json - 通过 AJAX Post 使用 JSON 将数据传递到 CFC 函数

c# - 身份验证表单登录 url 和路由数据

c# - dotnetopenauth OAuth2AuthorizationServer 示例修改 ResourceServerEncryptionPublicKey

java - 出于某种原因,我的 if 语句没有触发

java - 线程未在android中运行

android 如何同步 onitemclicklistener listview id 和数据库 id

.net - wcf 绑定(bind)接收超时

asp.net-mvc - 在 POST 编辑中保存表单的正确方法?