Search code examples
android-ksoap2

Send JSON Array to .net server using KSOAP library in android


I want to send JSON object / JSON Array to .net Serverusing KSOAP library.

Here is my code

sendJSON{

 JSONObject json = new JSONObject(); 
        try {  
        CallingSoap cs=new CallingSoap();

        String macid="1";
        String latStr= StaticVariableClass.latitude_str;
        String longStr= StaticVariableClass.longitude_str;
        String datetimeStr="23/04/2015";

            json.put("MacID",macid); 
            json.put("DateTime",datetimeStr);
            json.put("Latitude",latStr);
            json.put("Longitude",longStr );

            String  JSONString= json.toString();

            Log.e("JSON", JSONString);
    //       String resp=cs.demo("test");
             String resp=cs.demo(json);  // I need to send this json to my asp.net web server
                Log.d("Response from server",resp);


        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } 



//CallingSoap .java


public class CallSoap {
    public String demo(JSONObject a)
    //public String demo(String a)
    {
                final String SOAP_ACTIONS= "http://tempuri.org/GetLatLongJson";
                final String OPERATION_NAMES= "GetLatLongJson";
                final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
                final String SOAP_ADDRESS = "http://10.208.36.33/samtadoot2/samtadootwebservice.asmx";

        SoapObject request=new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAMES);
        PropertyInfo pi=new PropertyInfo();
        pi.setName("jsonobject");
        pi.setValue(a);
        pi.setType(JSONObject.class);
        request.addProperty(pi);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        new MarshalBase64().register(envelope);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        Object response=null;

        try
        {
            httpTransport.call(SOAP_ACTIONS, envelope);
            response = envelope.getResponse();
        }
        catch(Exception ex)
        {
            response=ex.toString();
        }
        //JSONArray JSONArray = null;
        return response.toString();


    }


}

it is throwing an exception of cannot seralize 04-27 12:52:46.378: D/Response from server(5982): java.lang.RuntimeException: Cannot serialize: {"MacID":"1","Latitude":"18.5647613","Longitude":"73.8069672"}

Thanks in advance


Solution

  • that:

    pi.setType(JSONObject.class);
    

    will not work, becouse there is no standard serialization defined for JSONObject in ksoap2. But what i suppouse what You didnt written is that You try to send JSON string to theserver. It i'm right, then You have to change Your code to send String and encode JSONObject to JSON string using toString this way:

    SoapObject request=new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAMES);
    PropertyInfo pi=new PropertyInfo();
    pi.setName("jsonobject");
    pi.setValue(a.toString());
    pi.setType(String.class);
    request.addProperty(pi);
    

    If i'm wrong, then You have to parse whole JSONObject to structure of SoapObjects/Primitives.

    Regards, Marcin