如何通过PhoneGap调用C#的WebService
2个回答
2016-01-18 · 知道合伙人软件行家
关注
展开全部
使用PhoneGAP调用webservice跟原生sdk方式不太一样,你需要定义一个plugin然后调用C#的webservice
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.");
}
}
}
WebServiceClient 类通过ksoap2去调用C#的webservice
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}]);
}
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.");
}
}
}
WebServiceClient 类通过ksoap2去调用C#的webservice
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}]);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询