我的代码如下:
public void testWebServiceForJson() {
TextView textView = new TextView(this);
setContentView(textView);
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
OPERATION_NAME);
ArrayList<String> list = new ArrayList<String>();
list.add("blah");
list.add("bleh");
JSONArray jsArray = new JSONArray(list);
String mStringArray[] = { "String1", "String2" };
JSONArray mJSONArray = new JSONArray(Arrays.asList(mStringArray));
request.addProperty("jsonArray", mJSONArray);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
try
{
httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
// SoapObject result = (SoapObject) envelope.bodyIn;
textView.setText("fafaf");
}
catch (Exception exception)
{
exception.printStackTrace();
textView.setText(exception.toString());
}
}
抛出异常:
03-02 17:33:18.441: WARN/System.err(520): java.lang.RuntimeException: Cannot serialize: ["String1","String2"]
03-02 17:33:18.451: WARN/System.err(520): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:661)
03-02 17:33:18.451: WARN/System.err(520): at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:645)
03-02 17:33:18.451: WARN/System.err(520): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:614)
03-02 17:33:18.461: WARN/System.err(520): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:598)
03-02 17:33:18.461: WARN/System.err(520): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:655)
03-02 17:33:18.461: WARN/System.err(520): at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:579)
03-02 17:33:18.461: WARN/System.err(520): at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:192)
03-02 17:33:18.471: WARN/System.err(520): at org.ksoap2.transport.Transport.createRequestData(Transport.java:74)
03-02 17:33:18.471: WARN/System.err(520): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:58)
03-02 17:33:18.471: WARN/System.err(520): at com.ey.web.AndroidClientService.testWebServiceForJson(AndroidClientService.java:69)
03-02 17:33:18.480: WARN/System.err(520): at com.ey.web.AndroidClientService.onCreate(AndroidClientService.java:29)
03-02 17:33:18.480: WARN/System.err(520): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-02 17:33:18.480: WARN/System.err(520): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-02 17:33:18.480: WARN/System.err(520): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-02 17:33:18.480: WARN/System.err(520): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-02 17:33:18.480: WARN/System.err(520): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-02 17:33:18.490: WARN/System.err(520): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 17:33:18.490: WARN/System.err(520): at android.os.Looper.loop(Looper.java:123)
03-02 17:33:18.490: WARN/System.err(520): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-02 17:33:18.490: WARN/System.err(520): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 17:33:18.490: WARN/System.err(520): at java.lang.reflect.Method.invoke(Method.java:521)
03-02 17:33:18.490: WARN/System.err(520): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-02 17:33:18.502: WARN/System.err(520): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-02 17:33:18.502: WARN/System.err(520): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
您不必使用ksoap2将json发送到服务器。 Ksoap习惯于使用soap webservice。
Json使用网络服务,好吧,json http://en.wikipedia.org/wiki/Json
将json发送到服务器:
Reader r;
String url = "http://10.111.111.43:9000/XentivoCrm/services/VisitJSON";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(url);
InputStream data = null;
try {
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
String json = mapper.writeValueAsString(mStringArray);
//I used jackson to create json, but built-in json.org will work
StringEntity se = new StringEntity(json);
httpost.setEntity(se);
httpost.setHeader("json", "application/json");
httpost.setHeader(HTTP.CONTENT_TYPE, "application/json");
HttpResponse response = httpClient.execute(httpost);
}