将jsondict保存到字符串值不正常

时间:2017-06-09 08:51:52

标签: ios objective-c json xcode

嗨我正在尝试向服务器发送请求,我正在尝试使用服务器的回复设置三个标签的值

我的代码: -

 NSString *post = [NSString stringWithFormat:@"phone=%@",self.tmvp];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//Next up, we read the postData's length, so we can pass it along in the request.
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
// Now that we have what we'd like to post, we can create an NSMutableURLRequest, and include our postData
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://test.kre8tives.com/barebon/customer_dataapi.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSLog(@"the data Details is %@", post);
//   And finally, we can send our request, and read the reply by creating a new NSURLSession:
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
static NSURLSession* sharedSessionMainQueue = nil;
if(!sharedSessionMainQueue){
    sharedSessionMainQueue = [NSURLSession sessionWithConfiguration:nil delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
}

[[sharedSessionMainQueue dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; // this is json string
    //  NSError *error;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; // you need to convert to dictionary object
    NSDictionary *temp=jsonDict;
    NSLog(@"Req cust:%@",requestReply);
    NSLog(@"requestReply cust: %@", jsonDict);
    self.names=[temp valueForKey:@"customer_name"];
    [self.namel setText:[NSString stringWithFormat:@"%@", self.names]];

   // _namel.text = [NSString stringWithFormat:@"%@",self.names];
    self.emails=[temp valueForKey:@"email"];
    _emaill.text=[NSString stringWithFormat:@"%@",self.emails];
    self.dobs=[temp valueForKey:@"dob"];
    _dobl.text=[NSString stringWithFormat:@"%@",self.dobs];
    self.iname=[temp valueForKey:@"image"];
    NSLog(@"%@",self.names);
    NSLog(@"%@",self.emails);
    NSLog(@"**%@",self.dobs);
    NSLog(@"requestReply: %@", requestReply);
    NSLog(@"imagename:%@",self.iname);
    [self imagedl];
    NSString *valueToSave =[jsonDict valueForKey:@"customer_id"];
    [[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"custid"];
    [[NSUserDefaults standardUserDefaults] synchronize];

}] resume];

首先我发送电话号码,然后从服务器

获取姓名,电子邮件,dob等

这是我的日志

2017-06-09 14:14:50.006 MenuBar[4127:203694] (
Akshay
)
2017-06-09 14:14:51.503 MenuBar[4127:203694] (
"akshay1318a@yahoo.com"
)
2017-06-09 14:14:51.503 MenuBar[4127:203694] **(
"18-01-1995"
)
2017-06-09 14:14:53.041 MenuBar[4127:203694] requestReply:   [{"customer_id":"111","customer_name":"Akshay","email":"akshay1318a@yahoo.com","phone":"9047038606","dob":"18-01-1995","image":"+919047038606cache.png","level":"0","created_date":"2017-06-07 11:12:55","current_store":"0"}]
2017-06-09 14:14:53.042 MenuBar[4127:203694] imagename:(
"+919047038606cache.png"
)
2017-06-09 14:14:53.041 MenuBar[4127:203694] requestReply: [{"customer_id":"111","customer_name":"Akshay","email":"akshay1318a@yahoo.com","phone":"9047038606","dob":"18-01-1995","image":"+919047038606cache.png","level":"0","created_date":"2017-06-07 11:12:55","current_store":"0"}]

这是我的回复,但为什么我在这里得到一个支架?因为标签只显示支架!!!在服务器中也没有添加支架我双重检查!!

0 个答案:

没有答案