如何通过PhoneGap调用C#的WebService?-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

如何通过PhoneGap调用C#的WebService?

a123456678 2016-07-11 16:19:32 1142

我想通过在Java EE上搭建的PhoneGap中调用WebService,用的是Jquery Mobile Android框架,直接通过JS的方法来调用Webservice在Android模拟器上运行,结果不支持。请专家们、大神们告诉我怎么调用?万分感激,本人刚接触,有点小白~~

移动开发 JavaScript Java C# Android开发
分享到
取消 提交回答
全部回答(1)
  • a123456678
    2019-07-17 19:54:19

    使用PhoneGAP调用webservice跟原生sdk方式不太一样,你需要定义一个plugin然后调用C#的webservice

    下面是我的一个程序里调用的一个人脸识别的例子 你可以参考一下 不明白可以Q我 550876005

    plugin 类

    ?

    package com.sdses.plugin;
     
    import org.apache.cordova.api.CallbackContext;
    import org.apache.cordova.api.CordovaPlugin;
    import org.apache.cordova.api.LOG;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
     
     
    public class FacePlugin extends CordovaPlugin {
     
        public static final String ACTION_CHECK_FACE_ENTRY = "checkFaceEntry"; 
         
        @Override
        public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
            try {
                if (ACTION_CHECK_FACE_ENTRY.equals(action)) { 
                    JSONObject arg_object = args.getJSONObject(0);
    //              String base64 = args.getString(0); //这里接收页面参数,按传参的顺序  
    //              String userId = args.getString(1); //这里接收页面参数,按传参的顺序  
                    this.echo(arg_object.getString("base64"), arg_object.getString("userId"), callbackContext); //传参时把当前的回调上下文传过去,必不可少  
                   return true;
                }
                callbackContext.error("Invalid action");
                return false;
            } catch(Exception e) {
                LOG.e("plugin", e.getMessage());
                System.err.println("Exception: " + e.getMessage());
                callbackContext.error(e.getMessage());
                return false;
            } 
        }
         
        private void echo(String base64, String userId, CallbackContext callbackContext) { 
            if (base64 != null && base64.length() > 0 && userId!=null && userId.length()>0) {
                WebServiceClient webService = new WebServiceClient();
                String rst = webService.checkFace(base64, userId);
                callbackContext.success(rst); 
                //json对象也可以当作参数传回到页面  
               // JSONObject jsono = new JSONObject();             
               // jsono.put("message",message);  
               // callbackContext.success(jsono);  
      
            } else {  
                callbackContext.error("Expected one non-empty string argument.");  
            }  
        } 
    }
    package com.sdses.plugin;
     
    import java.io.IOException;
     
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    import org.xmlpull.v1.XmlPullParserException;
     
    public class WebServiceClient {
         
        //WSDL文档中的命名空间
        private static final String targetNameSpace="http://tempuri.org/";
        //WSDL文档中的URL
        //private static final String WSDL="http://192.168.30.222:8097/VprWebService.asmx?WSDL";
        private static final String WSDL="http://124.128.34.71:8097/VprWebService.asmx?WSDL";
         
        //需要调用的方法名(获得本天气预报Web Services支持的洲、国内外省份和城市信息)
        private static final String HelloWorld="HelloWorld";
        private static final String CheckFace="CheckFace";
         
        public String getHello() throws IOException, XmlPullParserException {
            String str="";
            SoapObject soapObject=new SoapObject(targetNameSpace,HelloWorld);
            //request.addProperty("参数", "参数值");调用的方法参数与参数值(根据具体需要可选可不选)
             
            SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet=true;
            envelope.setOutputSoapObject(soapObject);//envelope.bodyOut=request;
             
            HttpTransportSE httpTranstation=new HttpTransportSE(WSDL);
            //或者HttpTransportSE httpTranstation=new HttpTransportSE(WSDL);
            //try {
                 
                httpTranstation.call(targetNameSpace+HelloWorld, envelope);
                Object result=(Object)envelope.getResponse();
                //下面对结果进行解析,结构类似json对象
                //str=(String) result.getProperty(6).toString();
                return result.toString();
    //        } catch (IOException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //            return "IOException";
    //        } catch (XmlPullParserException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //            return "XmlPullParserException";
    //        }
        }
         
        public String checkFace(String bmpBuffer, String userId){
            String str="";
            SoapObject soapObject=new SoapObject(targetNameSpace,CheckFace);
            soapObject.addProperty("base64", bmpBuffer);  //参数1   图片名  
            soapObject.addProperty("userId", userId);   //参数2  图片字符串 
             
            SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet=true;
            envelope.setOutputSoapObject(soapObject);//envelope.bodyOut=request;
             
            HttpTransportSE httpTranstation=new HttpTransportSE(WSDL);
            //或者HttpTransportSE httpTranstation=new HttpTransportSE(WSDL);
            try {
                 
                httpTranstation.call(targetNameSpace+CheckFace, envelope);
                Object result = envelope.getResponse();
                //下面对结果进行解析,结构类似json对象
                //str=(String) result.getProperty(6).toString();
                return result.toString();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return e.getMessage();
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return e.getMessage();
            }
        }
         
    }
    
    
    在res/xml/config.xml中添加这个plugin
    <feature name="FacePlugin">
          <param name="android-package" value="com.sdses.plugin.FacePlugin" />
        </feature>
    
    
    在html中调用如下   
    
    function RequestWebService(base64, userId) {
     cordova.exec(successFunction, failFunction, "FacePlugin","checkFaceEntry",[{"base64": base64, "userId": userId}]);
     }
    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题
推荐课程