Android calls the WCF service

Article Directory

This blog is a simple example of Android call a WCF service.

Examples

This example creates a simple notice of a WCF interface as an example about the interaction of Android and WCF.
First, the method can be used CreateNotice WcfTestClient WCF interface under test, Java can refer to Part I call a WCF blog.
New WCFService call a WCF tool class as follows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.carlos.wcf;

import android.os.Handler;
import android.os.Message;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


* Access WCF Tools
* the Created by Carlos ON 2015/11/28.
* /
Public class { public static Final String WCF_URL = "" ; // class changed their quotes public static Final String NAMESPACE = "" ; // quotes class changed his public static Final String the SOAP = "" ; // quotes classes changed their Private static Final ExecutorService ExecutorService = Executors.newFixedThreadPool ( 3 );






* Call WCF method
* @param methodName
* @param the Properties
* @param wcfServiceCallBack
* / public static void callWCFService ( Final String methodName, the Map the Properties, Final WCFServiceCallBack wcfServiceCallBack) { // create HttpTransportSE object, passing WCFService server address Final HttpTransportSE httpTransportSE = new new HttpTransportSE (WCF_URL); soapObject soapObject = new new soapObject (the NAMESPACE, methodName); IF (= Properties!





null){
for(Iterator<Map.Entry<String,Object>> it=properties.entrySet().iterator();it.hasNext();){
Map.Entry<String,Object> entry=it.next();
soapObject.addProperty(entry.getKey(),entry.getValue());
}
}

// instantiate SoapSerializationEnvelope, the incoming SOAP WebService protocol version Final SoapSerializationEnvelope the SOAPEnvelope = new new SoapSerializationEnvelope (SoapEnvelope.VER11); soapEnvelope.bodyOut = soapObject; // set whether to call a .Net development WCFService soapEnvelope.setOutputSoapObject (soapObject ); soapEnvelope.dotNet = to true ; httpTransportSE.debug = to true ;







Handler // child thread for communicating with the main thread Final Handler mHandler = new new Handler () { public void the handleMessage (the Message MSG) { Super .handleMessage (MSG); // the return value of the callback parameter to callBack wcfServiceCallBack.callBack ((SoapObject) msg.obj); } }; // open thread to access WCFService ExecutorService.submit ( new new the Runnable () { public void RUN () { SoapObject resultSoapObject = null ; the try { httpTransportSE.call (methodName + the SOAP, SoapEnvelope ); IF















(! soapEnvelope.getResponse () = null ) {
resultSoapObject = (SoapObject) soapEnvelope.bodyIn;
}
} the catch (IOException E) {
e.printStackTrace ();
} the catch (XmlPullParserException E) {
e.printStackTrace ();
} the finally { // Handler using the acquired message is sent to the main thread mHandler.sendMessage (mHandler.obtainMessage ( 0 , resultSoapObject)); } } }); }








Callback Interface *
* / public interface WCFServiceCallBack { public void callBack (SoapObject Result) ; } }




MainActivity class which invokes this method to print out the results returned WCF

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.carlos.wcf;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.ksoap2.serialization.SoapObject;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

private Button b_get_WCFResult;
private final static String TAG="WCFResult";


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b_get_WCFResult= (Button) findViewById(R.id.b_get_WCFResult);

b_get_WCFResult.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
getWCFResult();
}
});

}

private void getWCFResult(){
HashMap<String,Object> map=new HashMap<>();
map.put("title", "提醒");
map.put("content", "晚上写篇博客");
WCFService.callWCFService("CreateNotice", map, new WCFService.WCFServiceCallBack() {

public void callBack(SoapObject result) {
if(result!=null){
Toast.makeText(MainActivity.this,result.toString(),Toast.LENGTH_SHORT).show();
Log.i(TAG, result.toString());

SoapObject soapObject=(SoapObject)result.getProperty("result");
Log.i(TAG,soapObject.toString());
if(soapObject.toString().equals("anyType{}")){
Log.i(TAG,"没有结果");
}else{
for(int i=0;i<soapObject.getPropertyCount();i++){
Log.i("WCFResult",soapObject.getProperty(i).toString());
}
}

}
}
});
}
}

点击获取WCF结果的按钮后控制台输出的结果

原文:大专栏  Android调用WCF服务


Guess you like

Origin www.cnblogs.com/petewell/p/11607171.html