开发者

How to Pass json array as Parameter To A Webservice Using Ksoap2 in android

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开发者_Python百科: 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)


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:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜