Eclipse を使用して Android アプリケーションを作成しようとしていますksoap
。クラス オブジェクトを Web サービスのメソッドのパラメーターとして送信し、リストを応答として取得したいと考えています。これらのlink1 link2を見つけまし たが、機能するコードを書くことができませんでした。
これが私のコードです:
package com.testcustomer11;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity
{
private static String SOAP_ACTION1 =
"http://tempuri.org/GetCustomer";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME1 = "GetCustomer";
private static String URL = "http://localhost:8000/Service?wsdl";
Button button1;
EditText editText2;
TextView TextView01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
editText2 = (EditText)findViewById(R.id.editText2);
TextView01 = (TextView)findViewById(R.id.TextView01);
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
Customer C = new Customer();
request.addProperty("customer",editText2.getTag());
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
envelope.addMapping(NAMESPACE, "Customer",new Customer().getClass());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION1, envelope);
// Get the SoapResult from the envelope body.
SoapObject response = (SoapObject)envelope.bodyIn;
SoapObject soapResult = (SoapObject)response.getProperty(0);
for (int i = 0; i < soapResult.getPropertyCount(); i++) {
SoapObject res = (SoapObject) response.getProperty(i);
C.Code = res.getProperty(0).toString();
C.Name = res.getProperty(1).toString();
C.VATNo = res.getProperty(2).toString();
C.PriceList = res.getProperty(3).toString();
SoapObject so = (SoapObject) soapResult.getProperty(i);
so.getProperty("Code");
}
TextView01.setText(response.toString());
}
catch (Exception e)
{
//TextView01.setText("EXCEPTION");
e.printStackTrace();
}
}
});
}
}
および Customer クラス:
package com.testcustomer11;
import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
public class Customer extends MainActivity implements KvmSerializable
{
public int Code;
public String Name;
public String VATNo;
public String PriceList;
public Customer(){}
public Customer(int cCode, String cName, String cVATNo, String cPriceList) {
Code = cCode;
Name = cName;
VATNo = cVATNo;
PriceList = cPriceList;
}
public Object getProperty(int arg0) {
switch(arg0)
{
case 0:
return Code;
case 1:
return Name;
case 2:
return VATNo;
case 3:
return PriceList;
}
return null;
}
public int getPropertyCount() {
return 3;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
switch(index)
{
case 0:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Code";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Name";
break;
case 2:
info.type = PropertyInfo.STRING_CLASS;
info.name = "VATNo";
break;
case 3:
info.type = PropertyInfo.STRING_CLASS;
info.name = "PriceList";
break;
default:break;
}
}
public void setProperty(int index, Object value) {
switch(index)
{
case 0:
Code = Integer.parseInt(value.toString());
break;
case 1:
Name = value.toString();
break;
case 2:
VATNo = value.toString();
break;
case 3:
PriceList = value.toString();
break;
default:
break;
}
}
}
エミュレータで実行すると、このメッセージが表示されます"GetCustomerResponse{GetCustomerResult=anyType{};}"
。
何か案は??