编程语言
首页 > 编程语言> > 如何在Android应用程序中调用ASMX Web服务VIA JSON对象

如何在Android应用程序中调用ASMX Web服务VIA JSON对象

作者:互联网

我正在开发一个android应用程序…
我想通过json对象将数据传递给asmx webservice,并且我希望web服务能够响应并将数据访问到应用程序.

谁能告诉我这怎么可能?

我的代码是:

    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();

解决方法:

您可以使用内置的apache类(例如HttpClient,HttpGet,HttpPost等)来代替kSOAP库.

例如:您要发送以下JSON对象:

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

那么您可以使用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"));

标签:android,json,web-services,asmx
来源: https://codeday.me/bug/20191011/1890767.html