How to Call ASMX Web service VIA JSON object in an

2020-04-17 06:44发布

问题:

I am developing an android application... and I want to pass the data to asmx webservice via json object and I want the response of web service and access the data to the application.

Can anyone tell me how it is possible?

My Code Is:

    Thread myThread = new Thread()
{
    @Override
    public void run()
    {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("email",s1);
        request.addProperty("password",s2);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;
        try {
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapObject result = (SoapObject)envelope.bodyIn;
            if(result != null){
                et_unm.setText(result.getProperty(0).toString());
            } else
                Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
        }catch (Exception e){
            Log.v("LOGIN::","Exception--->"+e.toString());
            e.printStackTrace();
        }
    }
};
myThread.start();

回答1:

Instead of using kSOAP library, you can do it with built-in apache classes like HttpClient, HttpGet, HttpPost, etc.

For example: You want to send below JSON object:

{
   "email":"test@test.com",
   "password":"test"
}

then you can set your request object by using setEntity():

final String body = String.format("{\"email\":\"%s\",\"password\":\"%s\"}", s1, s2);

HttpClient client = new DefaultHttpClient();
HttpPost postMethod = new HttpPost(webServiceUrl);
postMethod.setEntity(new StringEntity(body, "utf-8"));