如何在 android 中使用 Ksoap2 将 json 数组作为参数传递给 Web 服务

发布于 2024-10-20 02:04:17 字数 4165 浏览 3 评论 0原文

我的代码如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若相惜即相离 2024-10-27 02:04:17

您不必使用 ksoap2 将 json 发送到服务器。 Ksoap 用于与 SOAP Web 服务进行通信。

将 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);
    }

You don't have to use ksoap2 to send json to server. Ksoap is used to comunicate with soap webservice.

To send json to server:

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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文