我试图将json结构发送到Web服务器

时间:2014-02-19 23:15:03

标签: ios json nsjsonserialization

我正在尝试将json结构发送到Web服务器 但是,每次我得到恐怖

这是我的代码,希望你能帮助我:

首先,我应该将结构发送到服务器:

{
   "UDID": "UDID - limited to 5 calls per day" 
   "Hazard": {
     "TypeID": 3, 
     "Info": "Some Information" 
     "Name": "User Name", 
     "Phone": "User Phone" 
     "Email": "User Email" 
     "FBID": "User FaceBook ID", 
     "LocationLat": "LocationLat", 
     "LocationLng": "LocationLng", 
     "LocationDesc": "Location Description", 
     "Image": "a Base64 Image" 
     "Extension": "jpg, gif, etc ..." 
   } 
} 

响应成功

{
  "error": 0,
  "Serial": "string : some serial number"
}

响应失败

{
  "error": 1,
  "errdesc": "Invalid Lat/Lng ,Type ID, etc..."
}

现在,你肯定会看到这两个库,所以,这里创建了它们:

这里我们创建库

NSMutableDictionary * Hazard; 
     NSMutableDictionary * maindic; 
NSString  TypeID = @ "TypeID"; 
    NSString  info = @ "info"; 
    NSString  Name = @ "Name"; 
   NSString   Phone = @ "Phone"; 
  NSString    Email = @ "Email"; 
   NSString   FBID = @ "FBID"; 
    
   NSString   LocationLat = @ "LocationLat"; 
    NSString  LocationLng = @ "LocationLng"; 
    NSString  LocationDesc = @ "LocationDesc"; 
    NSString  Image = @ "Image"; 
    NSString  Extension = @ "Extension"; 
    
    
     Hazard = [[NSMutableDictionary alloc] initWithObjectsAndKeys: 0, TypeID, @ "", info, @ "", Name, @ "", Phone, @ "", Email, @ "", FBID, @ "", LocationLat, @ "", LocationLng, @ "", LocationDesc, @ "", Image, @ "", Extension, nil]; 

危险决定:

[Hazard setValue: textField.text forKey: Name];

电话,图片......等等

现在问题在于,当用户点击按钮时,以下代码被激活...

NSString *uuidString = [[NSUUID UUID] UUIDString];



    maindic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:uuidString,@"UDID", Hazard, @"Hazard", nil];

    NSData *DATA = [NSJSONSerialization dataWithJSONObject:maindic options:NSJSONWritingPrettyPrinted error:0];


    NSLog(@"JSON summary2: %@", [[NSString alloc] initWithData:DATA
                                                      encoding:NSUTF8StringEncoding]);

NSURL *url = [[NSURL alloc]initWithString:@"myserverblalal.com"];


    NSString *data_length = [[NSString alloc]initWithFormat:@"%lu", (unsigned long)[DATA length]];



    NSMutableURLRequest *MyRequest = [[NSMutableURLRequest alloc]initWithURL:url
                                                                     cachePolicy:0 timeoutInterval:60];

    [MyRequest setHTTPMethod:@"POST" ];
    [MyRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [MyRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [MyRequest setValue:data_length forHTTPHeaderField:@"Content-Length"];


    [MyRequest setHTTPBody:DATA] ;


    NSURLResponse *response;


    NSData *POSTRE = [NSURLConnection sendSynchronousRequest:MyRequest returningResponse:&response error:nil];
    NSString *theReply = [[NSString alloc] initWithBytes:[POSTRE bytes] length:[POSTRE length] encoding: NSASCIIStringEncoding];
    NSLog(@"Reply: %@", theReply);

这是我从服务器获得的响应

回复:{“错误”:1,“errdesc”:“无效的UDID”}

有没有人可以帮助我,并告诉我为什么我会得到这个错误

由于

1 个答案:

答案 0 :(得分:0)

显然服务器不喜欢你发送它的UUID。

请注意,您创建了一个UUID(它只是一个随机的128位数字编码为字符串),但密钥是“UDID”,错误会引发无效的“UDID”。 UDID是唯一设备标识符,它是标识一个特定设备的代码,例如一个特定的iPhone。 UUID是一个普遍唯一的标识符 - 完全不同的东西。

出于隐私原因,不再支持UDID;作为替代,您将计算一次UUID并将其存储在UserDefaults中。

您最好与运行Web服务器的人员交谈并了解他们的期望。