废话不多说,直接入正题,供各位朋友参考~!
注意:服务器使用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 1011 12 13 14 26 27 2815 2516 17 2418 19 20 21 22 2329 30 31 32 38 84 8539 40 46 5141 44 4542 43 52 64 65 66 67 71 7268 7069 73 7874 75 76 77 79 81 82 8380 86 94 9587 9389 9290 91 96 124 12897 12398 104 113 118 119 122129 142 143 144130 131 133132 134 141135 137 139 140 145 178146 161147 148 160149 154 158155 157156 159 162 177163 168164 165 167166 169 176170 171 175172 173 174
客户端代码
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(); } } }}