Android调用RESTful WCF服务
WCF端
接口
[Description("REST服务测试")] [ServiceContract] public interface IAccountRestService : IRestServiceContract { [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)] List<Account> GetAccountDataByGet(); [WebInvoke(Method = "POST")] List<Account> GetAccountDataByPost(); /// <example>调用方式:/SendMessageByGet1?message=aaa&value=3</example> [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)] string SendMessageByGet1(string message, int value); /// <example>调用方式:/SendMessageByGet/aaa/3</example> [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "/SendMessageByGet2/{message}/{value}")] string SendMessageByGet2(string message, string value); /// <example>调用方式:{"message":"aa","value":3}。另外不要忘了在HTTP头中加入“content-type: text/json content-length: 26”。BodyStyle特性:方法参数若是多个,必须对其进行Json包装</example> [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] string SendMessageByPost1(string message, int value); }
实现
public class AccountService : IAccountRestService { static List<Account> AccountList { get { var list = new List<Account> { new Account { Name = "Bill Gates", Address = "YouYi East Road", Age = 56, Birthday = DateTime.Now }, new Account { Name = "Steve Paul Jobs", Address = "YouYi West Road", Age = 57, Birthday = DateTime.Now }, new Account { Name = "John D. Rockefeller", Address = "YouYi North Road", Age = 65, Birthday = DateTime.Now } }; return list; } } public List<Account> GetAccountDataByGet() { return AccountList; } public List<Account> GetAccountDataByPost() { return AccountList; } public string SendMessageByGet1(string message, int value) { return DateTime.Now + " GetMessage:" + message + value; } public string SendMessageByGet2(string message, string value) { return DateTime.Now + " GetMessage:" + message + value; } public string SendMessageByPost1(string message, int value) { return DateTime.Now + " PostMessage:" + message + value; } #region Implementation of IDisposable public void Dispose() { } #endregion }
托管配置
Bindings.Add(new WebHttpBinding { CrossDomainScriptAccessEnabled = true } , new List<IEndpointBehavior> { new WebHttpBehavior(), });
ANDROID端
界面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableLayout android:id="@+id/tableLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TableRow> <Button android:id="@+id/btnGetAccountDataByGet" android:text="GetAccountDataByGet" /> </TableRow> <TableRow> <Button android:id="@+id/btnGetAccountDataByPost" android:text="GetAccountDataByPost" /> </TableRow> <TableRow> <Button android:id="@+id/btnSendMessageByGet1" android:text="SendMessageByGet1" /> </TableRow> <TableRow> <Button android:id="@+id/btnSendMessageByGet2" android:text="SendMessageByGet2" /> </TableRow> <TableRow> <Button android:id="@+id/btnSendMessageByPost1" android:text="SendMessageByPost1" /> </TableRow> </TableLayout> </RelativeLayout>
后台
package com.example.testwcf; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.json.JSONObject; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); { Button button = (Button) super .findViewById(R.id.btnGetAccountDataByGet); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet( "http://10.168.1.102:9999/IAccountRestService/GetAccountDataByGet"); HttpResponse response = client.execute(request); Toast.makeText(MainActivity.this, EntityUtils.toString(response.getEntity()), Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } } }); } { Button button = (Button) super .findViewById(R.id.btnGetAccountDataByPost); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost( "http://10.168.1.102:9999/IAccountRestService/GetAccountDataByPost"); HttpResponse response = client.execute(request); Toast.makeText(MainActivity.this, EntityUtils.toString(response.getEntity()), Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } } }); } { Button button = (Button) super .findViewById(R.id.btnSendMessageByGet1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet( "http://10.168.1.102:9999/IAccountRestService/SendMessageByGet1?message=aaa&value=3"); HttpResponse response = client.execute(request); Toast.makeText(MainActivity.this, EntityUtils.toString(response.getEntity()), Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } } }); } { Button button = (Button) super .findViewById(R.id.btnSendMessageByGet2); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet( "http://10.168.1.102:9999/IAccountRestService/SendMessageByGet2/bbb/4"); HttpResponse response = client.execute(request); Toast.makeText(MainActivity.this, EntityUtils.toString(response.getEntity()), Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } } }); } { Button button = (Button) super .findViewById(R.id.btnSendMessageByPost1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost( "http://10.168.1.102:9999/IAccountRestService/SendMessageByPost1"); JSONObject p = new JSONObject(); p.put("value", 4); p.put("message", "ccc"); request.setEntity(new StringEntity(p.toString())); request.setHeader(HTTP.CONTENT_TYPE, "text/json"); HttpResponse response = client.execute(request); String re = EntityUtils.toString(response.getEntity()); Toast.makeText(MainActivity.this, re, Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } } }); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
截图
勉強心を持てば、生活は虚しくない!