与 Android 之间发送数据

发布于 2024-12-11 14:16:09 字数 3159 浏览 0 评论 0原文

我正在尝试从 Android 设备上的 .net 项目中获取数据。

在 android 上,我使用此代码:

    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://192.168.0.100/protobufnet/Default.aspx"); 

    HttpResponse response;
    response = client.execute(request);
    DataPacket.Data data = DataPacket.Data.parseFrom(response.getEntity().getContent());

DataPacket.Data 已从我手写的 .proto 文件自动生成,如下所示

message Data {
required int32 Status = 1;
message Building {
    required string Id = 1;
    optional string Name = 2;
    optional string Description = 3;
    message RentSpace {
        required string Id = 1;
        optional string Name = 2;
        optional string Description = 3;            
        message Year {
            required int32 Year = 1;
            message Item {
                required string Id = 1;
                optional string Name = 2;
                optional string Description = 3;        
                optional string FunctionDescription = 4;        
                required bool Marked = 5;
                required bool Remark = 6;
                required bool SelfLearning = 7;
                optional string Comment = 8;
                repeated bool ActiveMonths = 9 [packed=true];
            }
            repeated Item Items = 2;
        }
        repeated Year Years = 4;
    }
    repeated RentSpace RentSpaces = 4;
}
repeated Building Buildings = 4;

}

现在我不知道如何使我的 .net 项目针对此 .proto 文件进行验证,所以我刚刚编写了我认为相同的代码:

[ProtoContract]
public class DataPacket
{
    [ProtoMember(1)]
    public int Status { get; set; }
    [ProtoMember(2)]
    public List<Building> Buildings { get; set; }
}

[ProtoContract]
public class Building
{
    [ProtoMember(1)]
    public String Id {get;set;}
    [ProtoMember(2)]
    public String Name {get;set;}
    [ProtoMember(3)]
    public String Description {get;set;}
    [ProtoMember(4)]
    public List<RentSpace> RentSpaces { get; set; }
}

[ProtoContract]
public class RentSpace
{
    [ProtoMember(1)]
    public String Id {get;set;}
    [ProtoMember(2)]
    public String Name {get;set;}
    [ProtoMember(3)]
    public String Description {get;set;}
    [ProtoMember(4)]
    public List<YearList> Years {get;set;}
}

[ProtoContract]
public class YearList
{
    [ProtoMember(1)]
    public int Year;
    [ProtoMember(2)]
    public List<ListItem> Items {get;set;}
}

[ProtoContract]
public class ListItem
{
    [ProtoMember(1)]
    public String Id {get;set;}
    [ProtoMember(2)]
    public String Name {get;set;}
    [ProtoMember(3)]
    public String Description {get;set;}
    [ProtoMember(4)]
    public String FunctionDescription { get; set; } //how to do the control

    [ProtoMember(5)]
    public bool Marked { get; set; }
    [ProtoMember(6)]
    public bool Remark { get; set; }
    [ProtoMember(7)]
    public bool SelfLearning { get; set; }
    [ProtoMember(8)]
    public string Comment { get; set; }
    [ProtoMember(9)]
    public bool[] ActiveMonths { get; set; }

但它不起作用。我要么需要查看 .net 项目使用的 .proto,要么强制它根据我的 .proto 进行验证

I am trying to fetch data from my .net project on my android device.

On the android I am using this code:

    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://192.168.0.100/protobufnet/Default.aspx"); 

    HttpResponse response;
    response = client.execute(request);
    DataPacket.Data data = DataPacket.Data.parseFrom(response.getEntity().getContent());

The DataPacket.Data have been autogenerated from my .proto file that i wrote by hand and looks like this

message Data {
required int32 Status = 1;
message Building {
    required string Id = 1;
    optional string Name = 2;
    optional string Description = 3;
    message RentSpace {
        required string Id = 1;
        optional string Name = 2;
        optional string Description = 3;            
        message Year {
            required int32 Year = 1;
            message Item {
                required string Id = 1;
                optional string Name = 2;
                optional string Description = 3;        
                optional string FunctionDescription = 4;        
                required bool Marked = 5;
                required bool Remark = 6;
                required bool SelfLearning = 7;
                optional string Comment = 8;
                repeated bool ActiveMonths = 9 [packed=true];
            }
            repeated Item Items = 2;
        }
        repeated Year Years = 4;
    }
    repeated RentSpace RentSpaces = 4;
}
repeated Building Buildings = 4;

}

Now I dont know how to make my .net project validate against this .proto file, So I just coded what I assumed would be the same like this:

[ProtoContract]
public class DataPacket
{
    [ProtoMember(1)]
    public int Status { get; set; }
    [ProtoMember(2)]
    public List<Building> Buildings { get; set; }
}

[ProtoContract]
public class Building
{
    [ProtoMember(1)]
    public String Id {get;set;}
    [ProtoMember(2)]
    public String Name {get;set;}
    [ProtoMember(3)]
    public String Description {get;set;}
    [ProtoMember(4)]
    public List<RentSpace> RentSpaces { get; set; }
}

[ProtoContract]
public class RentSpace
{
    [ProtoMember(1)]
    public String Id {get;set;}
    [ProtoMember(2)]
    public String Name {get;set;}
    [ProtoMember(3)]
    public String Description {get;set;}
    [ProtoMember(4)]
    public List<YearList> Years {get;set;}
}

[ProtoContract]
public class YearList
{
    [ProtoMember(1)]
    public int Year;
    [ProtoMember(2)]
    public List<ListItem> Items {get;set;}
}

[ProtoContract]
public class ListItem
{
    [ProtoMember(1)]
    public String Id {get;set;}
    [ProtoMember(2)]
    public String Name {get;set;}
    [ProtoMember(3)]
    public String Description {get;set;}
    [ProtoMember(4)]
    public String FunctionDescription { get; set; } //how to do the control

    [ProtoMember(5)]
    public bool Marked { get; set; }
    [ProtoMember(6)]
    public bool Remark { get; set; }
    [ProtoMember(7)]
    public bool SelfLearning { get; set; }
    [ProtoMember(8)]
    public string Comment { get; set; }
    [ProtoMember(9)]
    public bool[] ActiveMonths { get; set; }

But it is not working. I either need to see the .proto the .net project uses or force it to validate against my .proto

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月光色 2024-12-18 14:16:10

protobuf-net 的 v1 zip 包含一个工具“protogen”,它基本上是 protobuf-net 的“protoc”(实际上它在内部使用 protoc 来执行某些步骤,因此大小也相同)。该工具在 v2 中没有变化,可以与 v2 库一起使用。

如果您使用 Visual Studio,还有一个 IDE 工具可以在工具内部执行相同的操作 - 即您只需将 .proto 添加到项目中,它就会为您生成匹配的 c#。

手动创建类型模型也是完全可能的 - 我需要坐下来仔细查看 .proto 和 c# 之间的差异(当然,比“但它不起作用”更具体一点“也会有帮助)。

当然,另一个明显需要检查的事情是(单独的)传输代码中的字节是否正确。

The v1 zip of protobuf-net includes a tool "protogen" that is basically protobuf-net's "protoc" (actually it uses protoc internally for some of the steps, hence the size). This tool is unchanged in v2, and is fine to use alongside the v2 library.

If you are usig Visual Studio there is also an IDE tool to do the same thing inside the tooling - i.e. you just add your .proto to a project, and it will generate the matching c# for you.

It is also fully possible to create the type-model manually - I would need to sit down and look very carefully to see the differences between your .proto and your c# (of course, being a bit more specific than "But it is not working" would help, too).

The other obvious thing to check, of course, is that you got the right byte in your (separate) transfer code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文