2012-03-27 64 views
1

我正在做一個wcf服務,將一個數據文件&的相關信息傳遞給一個android客戶端,然而,我是一個新的WCF,只有很多的閱讀和很多時間它不是清楚哪個是最好的方法!流媒體下載從WCF到android,使用Json,Rest或肥皂?

我應該處以何種格式對這個服務考慮到這將是一個Android設備調用呢?考慮我的代碼,以及什麼可以輕鬆實現,soap,json,rest或其他?

任何例子也將受到歡迎尤指用我下面定義的代碼。 謝謝!

[MessageContract] 
public class DownlaodStreamItem 
{ 

    [MessageHeader] 
    public Int64 ItemID { set; get; } 

    [MessageHeader] 
    public Int64 SizeOfFile { set; get; } 

    [MessageHeader] 
    public String Name { set; get; } 

    [MessageBodyMember] 
    public Stream Data { set; get; } 
} 

[MessageContract] 
public class someString 
{ 
    [MessageBodyMember] 
    public string SomeString{ set; get; } 
} 

服務

[OperationContract] 
    DownlaodStreamItem DownloadMessagecontact(someString SomeString); 

服務:IService

public DownlaodStreamItem DownloadMessagecontact(someString SomeString) 
{ 
    DownlaodStreamItem DLITEM = new DownlaodStreamItem(); 
    // Populate & return DownlaodStreamItem .... 
    return DLITEM; 
} 
+0

沒有注意到的流媒體需求。 – Aliostad 2012-03-27 10:50:51

回答

2

對於Android作爲客戶端和.NET的服務,最好的辦法是寫WCF REST服務。因爲在這種情況下,您不需要在android端創建任何代理類,只需發出Http請求即可使用它。

您可以使用WCF REST服務模板40(CS)輕鬆編寫REST WCF服務。 請參考this關於如何編寫它。

然後,你可以簡單地使用下面的代碼在Android的消費這個REST服務。希望這會幫助你。

try { 
      String URL; 
      URL = "http://localhost/MyRESTSvc/DownloadStreamItem";  

      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(URL); 
      InputStream inputStream = getResources().openRawResource(R.raw.task); 
      // Here task is any raw file you want to keep as input.. you may ignore it. 
      InputStreamEntity reqEntity = new InputStreamEntity(inputStream, -1); 
      reqEntity.setContentType("binary/octet-stream"); 
      reqEntity.setChunked(true); // Send in multiple parts if needed 
      httppost.setEntity(reqEntity); 

      HttpResponse response = httpclient.execute(httppost); 
      displayAlert("Success !!"); 
     } 
     catch (ClientProtocolException e) {     
      displayAlert(e.getMessage()); 
     } 
     catch (IOException e) {     
      displayAlert(e.getMessage()); 
     } 
     catch (Exception e) {     
      displayAlert(e.getMessage()); 
     }