如何使用 KSOAP2 从正文中获取数据 - android
How to get data from body using KSOAP2 - android
我正在尝试使用 ksoap2 库连接到 SOAP Web 服务。我已经阅读了很多关于它的文档,但最后他向我展示了空数组,请帮助我。
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>
这是我正在使用的 Web 服务
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);
}
}
和演示自定义模型 class:
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);
}
}
这对我有用。
我正在尝试使用 ksoap2 库连接到 SOAP Web 服务。我已经阅读了很多关于它的文档,但最后他向我展示了空数组,请帮助我。
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>
这是我正在使用的 Web 服务
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);
}
}
和演示自定义模型 class:
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);
}
}
这对我有用。