【问题标题】:Streaming download From WCF To android, Use Json, Rest or soap?从 WCF 流式下载到 android,使用 Json、Rest 还是 soap?
【发布时间】:2012-03-27 10:44:18
【问题描述】:

我正在开发一个 wcf 服务以将数据文件及其相关信息流式传输到 android 客户端,但是我还是 WCF 的新手,只有大量阅读和很多时候不清楚哪个是最好的方法!

考虑到它将是调用它的 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;
 }

【问题讨论】:

  • 没有注意到流媒体需求。

标签: c# json wcf rest soap


【解决方案1】:

对于 android 作为客户端和 .NET 作为服务,最好的方法是编写 WCF REST Service。因为在这种情况下你不需要在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());
        }

【讨论】:

    猜你喜欢
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2011-04-30
    • 1970-01-01
    • 2013-01-24
    • 2014-03-16
    • 2021-06-16
    相关资源
    最近更新 更多