iOS 5 Json Serialization

前端 未结 1 1975
攒了一身酷
攒了一身酷 2021-01-16 11:20

I have used JSON Serialization to get json response, here i\'mn getting all fine, but when i need to post some values as key value pair with the URL. I have done like this,

1条回答
  •  时光说笑
    2021-01-16 11:34

    There are multiple Errors in your Code, Use my Code as a Reference and compare it to yours and you'll get the Errors done by you. The Below code is working correctly from the Point of View of Objective-C. There are some Errors regarding your URL or Service Side.

    Working Code :

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"ann",@"uname",@"ann",@"pwd",@"login",@"req", nil];
    NSLog(@"dict :: %@",dict);
    NSError *error2;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error2];
    NSString *post = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    NSLog(@"postLength :: %@",postLength);
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://exemplarr-itsolutions.com/dbook/index.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setHTTPBody:postData];
    
    NSURLResponse *response;
    NSError *error3;
    NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error3];
    NSString *str = [[NSString alloc] initWithData:POSTReply encoding:NSUTF8StringEncoding];
    NSLog(@"str :: %@",str);
    

    0 讨论(0)
提交回复
热议问题