ios - 使用 Objective C 创建 JSON 数据并将其发送到服务器

标签 ios objective-c json xcode

我尝试使用 POST 方法将 JSON 数据发送到服务器端,但我的代码给出了 null JSON 值。我正在使用 Objective C 从 textField 获取数据并将其转换为字符串,但之后将此值转换为 JSON 对象时,它给出 null 值。不知道该怎么办。

这是我的代码:

- (IBAction)loginAction:(UIButton *)sender
{
NSString *post = [NSString stringWithFormat:@"Username=%@&Password=%@" ,self.userNameField.text,self.passwordField.text];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
postData = [postData subdataWithRange:NSMakeRange(0, [postData length] - 1)];
NSData*jsonData = [NSJSONSerialization JSONObjectWithData:postData options:NSJSONReadingMutableContainers error:nil];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://172.31.144.227:8080/Analytics/rest/login/post"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length" ];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:jsonData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[theConnection start];

NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
}

最佳答案

-(void)MessagePost{
NSString * post =[NSString stringWithFormat:@"http://url.com/clients/project_id=%@&user_id=58&question=%@&send_enquiry=Send",[[self.recordchat objectForKey:@"id"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[[_txtfield text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%@",post);
NSData *postdata= [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength=[NSString stringWithFormat:@"%lu",(unsigned long)[postdata length]];
NSMutableURLRequest *request= [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:post]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postdata];
NSError *error;
NSURLResponse *response;
postdata=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *returnstring=[[NSString alloc]initWithData:postdata encoding:NSUTF8StringEncoding];
NSLog(@"String : %@",returnstring);
if (postdata){
    NSDictionary *dict= [NSJSONSerialization JSONObjectWithData:postdata options:NSJSONReadingMutableContainers error:nil];
    NSDictionary* latestLoans = [dict objectForKey:@"status"];
    NSLog(@"Status dict = %@",latestLoans);
}  else{ NSLog(@"Error while posting messages.");}}

关于ios - 使用 Objective C 创建 JSON 数据并将其发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32758111/

相关文章:

ios - Skobbler 将自定义 POI 添加到热图中

ios - 调用 touchesEnded : when tapped but not swiped

javascript - 从 Javascript 数组创建 JSON 对象

javascript - 在 JavaScript 中将 JSON 数据转换为数组

ios - 将导航栏设置为半透明 : NO

ios - 添加 NSMutableDictonary 中特定键的值(使用 NSPredicate)

objective-c - 在一对一关系的核心数据删除过程中查找 NSManagedObject 时出现问题

javascript - 来自 localstorage 的 JSON 无法在 php 中解码

android - 是否可以在 Flutter 中用图像填充 ExpansionTile 标题?

ios - 是否可以在成功发送后为通知回复添加声音,例如 iMessage?