Skip to content

Commit

Permalink
update 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
guolf committed Oct 21, 2015
1 parent 1446ac2 commit 66d4e37
Showing 3 changed files with 141 additions and 9 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
# Cordova Soap plugin


Cordova使用Soap调用webservice。

安装:

```
cordova plugin add https://github.com/guolf/SoapHelper-Cordova-Plugin.git
```

删除

```
cordova plugin remove cordova-plugin-soaphelper
```

调用


```
try{
window.plugins.soaphelper.caller(method,par,value,success,error);
}catch(ex){
navigator.notification.alert("程序异常:"+ex.name + "," + ex.message);
}
function success(data){
if(data.code == 1){
//...
}else{
navigator.notification.alert(data.result);
}
}
function error(error){
navigator.notification.alert(error.result);
}
```
6 changes: 3 additions & 3 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -2,19 +2,19 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-soaphelper"
version="0.0.2">
version="1.0.0">

<name>SoapHelper</name>

<description>
This plugin help you easy user soap on the cordova
webservice soaphelper
</description>

<author>guolf</author>

<license>Apache 2.0</license>

<keywords>Soap,Phoengap,Cordova,WebServer</keywords>
<keywords>Soap,Phoengap,Cordova,webservice</keywords>

<repo>https://github.com/guolf/SoapHelper-Cordova-Plugin/.git</repo>

109 changes: 103 additions & 6 deletions src/android/SoapHelper.java
Original file line number Diff line number Diff line change
@@ -5,26 +5,123 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.net.SocketTimeoutException;

/**
* Author:guolf on 15/10/20 10:17
* Email :guo@guolingfa.cn
*/
public class SoapHelper extends CordovaPlugin{

private final String url = "http://192.168.1.100/service.asmx";
private final String namespace = "http://tempuri.org/";
private final int timeout = 10000;
private ProgressDialog pDialog;

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException{
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
try {
if (action.equals("caller")) {
JSONObject r = new JSONObject();
r.put("result"," this is a test from java code");
callbackContext.success(r);
if (!args.getString(0).equalsIgnoreCase("")) {
String[] par = args.getString(1).split("--");
String[] obj = args.getString(2).split("--");
callWebServer(args.getString(0), par, obj, callbackContext);
} else {
JSONObject r = new JSONObject();
r.put("code", 0);
r.put("result", "参数不正确");
callbackContext.error(r);
}
}
}catch (Exception ex){
} catch (Exception ex) {
JSONObject r = new JSONObject();
r.put("error",ex);
r.put("result", ex);
r.put("code", 0);
callbackContext.error(r);
}
return true;
}

private void callWebServer(final String methodName, final String[] parm, final String[] value, final CallbackContext callbackContext) {

AsyncTask task = new AsyncTask() {

@Override
protected Object doInBackground(Object[] params) {
JSONObject json = new JSONObject();
try {
String soapAction = namespace + methodName;
SoapObject request = new SoapObject(namespace, methodName);
for (int i = 0; i < parm.length; i++) {
request.addProperty(parm[i], value[i]);
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(url, timeout);
androidHttpTransport.call(soapAction, envelope);
if (envelope.getResponse() != null) {
try {
json.put("code", 1);
json.put("result", envelope.getResponse().toString());
} catch (JSONException j) {

}
}
} catch (ConnectException ex) {
try {
json.put("code", 0);
json.put("result", "网络异常");
} catch (JSONException j) {

}
} catch (XmlPullParserException ex) {
try {
json.put("code", 0);
json.put("result", "xml解析异常");
} catch (JSONException j) {

}
} catch (IOException ex) {
try {
json.put("code", 0);
json.put("result", "IO异常");
} catch (JSONException j) {

}
} catch (Exception ex) {
try {
json.put("code", 0);
json.put("result", ex.getMessage());
} catch (JSONException j) {

}
}
return json;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = ProgressDialog.show(cordova.getActivity(), null, "loading..");
}

@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
JSONObject json = (JSONObject) o;
callbackContext.success(json);
if (pDialog != null)
pDialog.dismiss();
}
};
task.execute();
}

}

0 comments on commit 66d4e37

Please sign in to comment.