将对象传递给WCF服务

时间:2013-08-08 07:22:05

标签: android wcf

我对WCF服务完全陌生。我不知道如何将复杂类型传递给WCF服务。

public class MainActivity extends Activity implements OnClickListener {
SoapObject result;
Button b;
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "InsertData";
private static String SOAP_ACTION = "http://tempuri.org/IWCFMasterTable/InsertData";
private static String URL = "http://172.16.0.1:55355/WCFMasterTable.svc";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.button1) {
        MyClass MC = new MyClass(MainActivity.this);
        MC.execute("");
    }
}

class MyClass extends AsyncTask<String, Void, SoapObject> {
    JSONObject jobj;
    MainActivity activity;

    public MyClass(MainActivity activity) {
        this.activity = activity;
    }

    @Override
    protected SoapObject doInBackground(String... params) {
        try {
            ClsMasterTable ct = new ClsMasterTable();
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            ct = ct.getData();
            System.out.println(ct.TableID);
            request.addProperty("args", ct);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER12);
            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;
            HttpTransportSE transport = new HttpTransportSE(URL);
            try {
                transport.call(SOAP_ACTION, envelope);
            } catch (Exception e) {
                e.printStackTrace();
            }

            SoapFault fault = (SoapFault) envelope.bodyIn;
            System.out.println("Fault : " + fault.toString());
            result = (SoapObject) envelope.bodyIn;
            System.out.println("result : " + result);
        } catch (Exception e) {
            System.out.println("Exception : " + e.toString());
        }
        return result;
    }

    @Override
    protected void onPostExecute(SoapObject result) {
        super.onPostExecute(result);
    }
}
}

public class ClsMasterCaseType {
String ID, Name, Mark;

public ClsMasterCaseType getData() {
    Mark= "88";
    ID = "CTYPAAAA0005";
    Name= "Karthik";
    return this;
}
}

这样我试图传递复杂类型。但这引发了异常

08-08 12:53:42.376: W/System.err(7845): java.lang.RuntimeException: Cannot serialize: 

com.elintsys.wcfservice.ClsMasterCaseType@40fcbdf0
08-08 12:53:42.376: W/System.err(7845):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:687)
08-08 12:53:42.376: W/System.err(7845):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:671)
08-08 12:53:42.386: W/System.err(7845):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:640)
08-08 12:53:42.386: W/System.err(7845):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:624)
08-08 12:53:42.386: W/System.err(7845):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:681)
08-08 12:53:42.386: W/System.err(7845):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:597)
08-08 12:53:42.386: W/System.err(7845):     at org.ksoap2.transport.Transport.createRequestData(Transport.java:101)

how to pass object in soap request我也尝试过这种方式。请帮帮我的朋友。我浪费了三天时间。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我对您的代码进行了一些更改。

<强> 1)

  

SoapSerializationEnvelope Envelope = new SoapSerializationEnvelope(                               SoapEnvelope.VER12);

改为

  

SoapSerializationEnvelope envelope = new   SoapSerializationEnvelope(SoapEnvelope.VER11);

<强> 2)

  

SoapObject response =(SoapObject)envelope.bodyIn;

改为

  

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