我是iOS开发的新手,我必须使用数组格式的POST
数据
我的服务器端代码是
$auth_query = array
(
'User' => array
(
'username' => 'example@example.com',
'password' => 'example'
)
);
我在Xcode中的api调用是
NSString *urlGetuser =[NSString stringWithFormat:@"http://example.com/login_api.json"];
NSURL *urlProducts=[NSURL URLWithString:urlGetuser];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:urlProducts];
[request setHTTPMethod:@"POST"];
NSMutableString *postData = [NSMutableString string];
NSMutableDictionary *user_details = [[NSMutableDictionary alloc]init];
[user_details setObject:@"example@example.com" forKey:@"username"];
[user_details setObject:@"example" forKey:@"password”];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:user_details forKey:@"User”];
NSString *usr_details = [NSString stringWithFormat:@"%@",dict];
[postData appendFormat:@"%@",usr_details];
NSData *body = [postData dataUsingEncoding:NSJSONReadingAllowFragments];
[request setHTTPBody: body];
NSURLResponse *response;
NSError *error;
NSData *aData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSMutableArray *jsonDATA=(NSMutableArray*)[NSJSONSerialization JSONObjectWithData:aData options:kNilOptions error:&error];
我无法事先发现问题
答案 0 :(得分:0)
实际上,对于发布方法,我们有不同的方法和最简单的方法之一,下面的代码有助于加载URL请求的数据,并在请求完成或失败时在操作队列上执行处理程序块。
if( [self setParams]){ NSOperationQueue *queue = [[NSOperationQueue alloc]init]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *urlResponse, NSData *data, NSError *error){ NSLog(@"image post Completed"); response.text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; [indicator stopAnimating]; [UIApplication sharedApplication].networkActivityIndicatorVisible = FALSE; if (error) { NSLog(@"error:%@", error.localizedDescription); } }]; }
答案 1 :(得分:0)
NSString *username = @“username";
NSString *password = @“password";
NSError *error;
NSHTTPURLResponse *response = nil;
NSDictionary *firstJsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys: username,@"email",password,@"password", nil];
NSDictionary *secondJsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys: firstJsonDictionary,@"user", nil];
NSMutableArray * arr = [[NSMutableArray alloc] init];
[arr addObject:secondJsonDictionary];
NSLog(@"Post contents %@",secondJsonDictionary);
NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:[arr objectAtIndex:0] options:NSASCIIStringEncoding error:&error];
NSString *urlGetuser =[NSString stringWithFormat:@“www.example.com"];
NSURL *urlProducts=[NSURL URLWithString:urlGetuser];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:urlProducts];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData2];
[request setHTTPShouldHandleCookies:NO];
NSData *aData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Error = %@",error);
if (aData)
{
NSMutableDictionary *json_DATA = (NSMutableDictionary *)[NSJSONSerialization JSONObjectWithData:aData options:NSASCIIStringEncoding error:&error];
NSLog(@"OUT Json %@",json_DATA);
}
[_activityindicator stopAnimating];
_overlayView.hidden = YES;
答案 2 :(得分:0)
由于您正在进行iOS开发,因此您开始处理一些不好的事情:1)您应该避免使用同步网络连接,因为它们可以冻结您的应用程序等待响应,2)您应该避免使用NSURLConnection新项目,因为NSURLSession方法将为您提供更大的灵活性,并能够进行后台传输甚至Apple Watch连接。要解决所有这些问题,请在AFNetworking获取并使用优秀的AFNetworking代码。无需重写其他更有经验的编码人员已编写的代码。