2011-10-25 4 views
1

내 안드로이드 기기에서 내 .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

내가 손으로 쓴이

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; 

}

처럼 보이는 내 .proto 파일에서 자동 생성되었습니다 안드로이드에

은이 코드를 사용하고

이제이 .proto 파일에 대해 .net 프로젝트의 유효성을 검사하는 방법을 모릅니다. 따라서 다음과 같이 가정합니다.

[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; } 

하지만 작동하지 않습니다. 나는 .proto .net 네트워크 프로젝트가 사용하거나 내 .proto에 대해 유효성을 검사하도록한다.

답변

1

protobuf-net의 v1 zip은 기본적으로 protobuf-net의 "protoc"도구이다. 일부 단계에서는 protoc을 내부적으로 사용하므로 크기를 사용합니다. 이 도구는 v2에서 변경되지 않았으며 v2 라이브러리와 함께 사용할 수 있습니다.

Visual Studio를 유용하게 사용하는 경우 도구 내에서 동일한 작업을 수행하는 IDE 도구가 있습니다. 즉, 프로젝트에 .proto를 추가하면 일치하는 C#이 생성됩니다.

수동으로 타입 모델을 만들 수도 있습니다. 저는 앉아서 여러분의 .proto와 여러분의 C# 사이의 차이점을보기 위해 아주 조심스럽게 살펴볼 필요가 있습니다. (물론 " 그것은 작동하지 않습니다 "도 도움이 될 것입니다).

확인해야 할 또 다른 명백한 점은 물론 (별도의) 전송 코드에 올바른 바이트가 있다는 것입니다.

+0

답변 해 주셔서 감사합니다. proto 파일에서 VS 도구를 사용했는데 한 가지 예외가 있습니다.이 줄은 .proto의 "Building Buildings = 4; 오직 set 속성을 얻지 못합니다. 그래서 건물 목록을 설정할 수 없습니다. –

+0

@Rickard 수집 속성이 get-only가되는 것은 꽤 보통입니다. 그냥/Add/Clear/AddRange 등을 가져 오거나 설정하려면 xslt를 편집 할 수 있습니다. –

+0

수 년 동안 .net을 완료하지 못했지만 인식하지 못했습니다. , 다시 감사합니다. 얼마나 빨리 답장을 보내시겠습니까 :-) –