如何在 android 中使用 Ksoap2 将 json 数组作为参数传递给 Web 服务
我的代码如下:
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)
My code is given below:
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());
}
}
An exception is thrown :
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必使用 ksoap2 将 json 发送到服务器。 Ksoap 用于与 SOAP Web 服务进行通信。
Soap 使用 xml
http://en.wikipedia.org/wiki/SOAP
Json Web 服务使用,嗯,json
http://en.wikipedia.org/wiki/Json
将 json 发送到服务器:
You don't have to use ksoap2 to send json to server. Ksoap is used to comunicate with soap webservice.
Soap use xml
http://en.wikipedia.org/wiki/SOAP
Json web-service use, well, json
http://en.wikipedia.org/wiki/Json
To send json to server: