博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第一篇 (wcf 与 android 传递基本数据类型与自定义数据类型)
阅读量:5740 次
发布时间:2019-06-18

本文共 8644 字,大约阅读时间需要 28 分钟。

废话不多说,直接入正题,供各位朋友参考~!

注意:服务器使用wcf rest风格,传递数据使用Json格式,但是并没有包装该格式,因为都是实现单参,双参必须要包装,当然单参也可以包装 ^_^

服务器端代码

wcf接口:

namespace Test{    // 注意: 如果更改此处的接口名称 "IService1",也必须更新 Web.config 中对 "IService1" 的引用。    [ServiceContract]    public interface IService1    {        [OperationContract]        [WebInvoke(UriTemplate="contact_entity",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,Method="POST")]        Person contact_entity(Person ps);        [OperationContract]        [WebInvoke(UriTemplate = "contact_entity_return_string", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "POST")]        string contact_entity_return_string(Person ps);             [OperationContract]        [WebInvoke(UriTemplate = "test1", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "POST")]        string test1(string pass_string);           }}

wcf接口实现

namespace Test{    // 注意: 如果更改此处的类名“Service1”,也必须更新 Web.config 和关联的 .svc 文件中对“Service1”的引用。    public class Service1 : IService1    {        #region IService1 成员        public Person contact_entity(Person ps)        {           // throw new NotImplementedException();            ps.username = "successful";            return ps;        }        public string contact_entity_return_string(Person ps)        {           // throw new NotImplementedException();            if (ps.username == "jwc")            {                return "successful";            }            else            {                return "failth";            }        }           public string test1(string pass_string)        {            pass_string = pass_string.Replace("{", "").Replace("}", "").Trim();            string[] arry = pass_string.Split(new char[1] { ':' });            if (arry[1] == "jwc")            {                return "successful";            }            else            {                return "faith";            }           }        #endregion    }}

Person代码:

namespace Test{    [DataContract]    public class Person    {        [DataMember]        public int id        {            get;            set;        }        [DataMember]        public string username        {            get;            set;        }        [DataMember]        public string password        {            get;            set;        }    }}

web.config

View Code
1 
2
10
11 12 13
14
15
16
17
18
19
20
21
22
23
24
25
26 27 28
29
30 31
32
38
39 40
41
42
43
44 45
46
51
52
64 65 66
67
68
69
70
71 72
73
74
75
76
77
78
79
80
81 82 83
84 85
86
87
89
90
91
92
93
94 95
96
97
98
104
113
118
119
122
123
124
128
129
130
131
132
133
134
135
137
139
140
141
142 143 144
145
146
147
148
149
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

 

 

客户端代码

 

public class Wcf_Json_PassActivity extends Activity {	private Button invok1_btn;	private Button invok2_btn;	private Button invok3_btn;	private EditText result_txt;	private String uri1 = "http://192.168.1.229/service1.svc/contact_entity";	private String uri2 = "http://192.168.1.229/service1.svc/contact_entity_return_string";	private String uri3 = "http://192.168.1.229/service1.svc/test1";	private HttpClient hc = null;	private HttpPost hp = null;	private HttpResponse hr = null;	/** Called when the activity is first created. */	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		findAll();		bind();	}	public void findAll() {		invok1_btn = (Button) this.findViewById(R.id.invok1_btn);		invok2_btn = (Button) this.findViewById(R.id.invok2_btn);		invok3_btn = (Button) this.findViewById(R.id.invok3_btn);		result_txt = (EditText) this.findViewById(R.id.result_txt);	}	public void bind() {		invok1_btn.setOnClickListener(mylistener);		invok2_btn.setOnClickListener(mylistener);		invok3_btn.setOnClickListener(mylistener);	}	Handler hd = new Handler() {		@Override		public void handleMessage(Message msg) {			// TODO Auto-generated method stub			// super.handleMessage(msg);			if (msg.what == 123) {				result_txt.setText(msg.obj.toString());			}		}	};	private View.OnClickListener mylistener = new OnClickListener() {		public void onClick(View v) {			// TODO Auto-generated method stub			switch (v.getId()) {			case R.id.invok1_btn:				JSONObject jo1 = new JSONObject();				try {					jo1.put("id", 11);					jo1.put("username", "jwc");					jo1.put("password", "123456");					Thread th1 = new Thread(new mythread(uri1, jo1.toString()));					th1.start();				} catch (JSONException e) {					// TODO Auto-generated catch block					e.printStackTrace();				}				// invok_wcf(uri1,)				break;			case R.id.invok2_btn:				JSONObject jo2 = new JSONObject();				try {					jo2.put("id", 11);					jo2.put("username", "jwc");					jo2.put("password", "123456");					Thread th2 = new Thread(new mythread(uri2, jo2.toString()));					th2.start();				} catch (JSONException e) {					// TODO Auto-generated catch block					e.printStackTrace();				}				break;			case R.id.invok3_btn:			        JSONObject jo3 = new JSONObject();				try {					jo3.put("pass_string", "jwc");				} catch (JSONException e) {					// TODO Auto-generated catch block					e.printStackTrace();				} 				Thread th3 = new Thread(new mythread(uri3,jo3.toString()));					th3.start();							break;			}		}	};	class mythread implements Runnable {		String my_uri;		String my_date;		public mythread(String uri, String date) {			this.my_uri = uri;			this.my_date = date;		}		public void run() {			// TODO Auto-generated method stub			hc = new DefaultHttpClient();			hp = new HttpPost(my_uri);			StringEntity se;			try {				se = new StringEntity(my_date, HTTP.UTF_8);				se.setContentType("application/json");				hp.setEntity(se);				hr = hc.execute(hp);				String strResp = null;				if (hr.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {					strResp = EntityUtils.toString(hr.getEntity());				} else {					strResp = "$no_found_date$";				}				Message msg = hd.obtainMessage(123);				msg.obj = strResp;				hd.sendMessage(msg);			} catch (UnsupportedEncodingException e) {				// TODO Auto-generated catch block				e.printStackTrace();			} catch (ClientProtocolException e) {				// TODO Auto-generated catch block				e.printStackTrace();			} catch (IOException e) {				// TODO Auto-generated catch block				e.printStackTrace();			} finally {	                           	hp.abort();	                                      }		}	}}

转载于:https://www.cnblogs.com/jason-jo/archive/2011/09/08/2171521.html

你可能感兴趣的文章
时间助理 时之助
查看>>
英国征召前黑客组建“网络兵团”
查看>>
Silverlight 2.5D RPG游戏“.NET技术”技巧与特效处理:(十二)魔法系统
查看>>
PHP 命令行模式实战之cli+mysql 模拟队列批量发送邮件(在Linux环境下PHP 异步执行脚本发送事件通知消息实际案例)...
查看>>
pyjamas build AJAX apps in Python (like Google did for Java)
查看>>
LAMP环境搭建1-mysql5.5
查看>>
centos5.9使用RPM包搭建lamp平台
查看>>
Javascript String类的属性及方法
查看>>
[LeetCode] Merge Intervals
查看>>
SharePoint 读取 Site Columns 的数据并绑定到DropdownList
查看>>
使用 axios 详解
查看>>
IPA提交APPStore问题记录(一)
查看>>
Ubuntu 14.04 vsftp refusing to run with writable root inside chroot问题解决方法
查看>>
Intellij IDEA远程调试tomcat
查看>>
hadoop的学习论坛
查看>>
Struts2 学习小结
查看>>
烂泥:wordpress迁移到docker
查看>>
.扒渣机的性能及优势 
查看>>
Linux下磁盘保留空间的调整,解决df看到的空间和实际磁盘大小不一致的问题
查看>>
RSA 生成公钥、私钥对
查看>>