如何在目标c中使用JSON服务POST数据
我已经做了一个登录表单。领域r电子邮件和密码。现在我想要将字段中的数据发布到特定的url,它是如何完成的。我对IOS完全陌生。有谁能够帮助我??如何做HTTP请求和JSON解析?如何在目标c中使用JSON服务POST数据
下面是代码,
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration
defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
// choose the right type for your value.
NSDictionary *postDict = @{@"key1": value1, @"key2": value2};
NSData *postData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil];
[request setURL:[NSURL URLWithString:@"SERVER URL"];
[request setHTTPBody:postData];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
if (!error) {
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// Work with dictionary
} else {
// parsing error code here
}
}];
[postDataTask resume];
/*********See this**********/
-(void)webServiceCall{
NSString *dataToSend = [NSString stringWithFormat:@"Username=%@&Password=%@“,<userIdEnter Here>,<Password enter here>];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *Length = [NSString stringWithFormat:@"%d",[postData length]];
[request setURL:[NSURL URLWithString:@“WEBURL”]];
[request setHTTPMethod:@"POST"];
[request setValue:Length forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
// check connection if you want
/*****get response in delegates*******/
- (void)connection:(NSURLConnection *)connection didReceiveResponse:
(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
{
/**************/
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
// NSArray* latestLoans = [json objectForKey:@"loans"];
NSLog(@"json: %@", json);
[_responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Error --> %@",error.localizedDescription);
/***************/
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
// use Result
self.responseData = nil;
}
在响应代表中写什么? @ajjjjjjjj – omer
认真地,我不明白它可以详细说明代码吗?在哪里编写代码我们如何获得响应以及在哪里编写响应委托? @ajjjjjjjj – omer
首先,您必须将代码粘贴到要调用Web服务的类中。现在,在**登录按钮**上单击,调用方法'webServiceCall',您必须更改用户输入的user_id和密码。发生呼叫后,将会调用'NSUrlConnection'委托(读取委托文档)。在'connectionDidFinishLoading'中得到响应后。你必须以这种方式解析数据'id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; '。之后,你可以使用'结果' – ajjjjjjjj
[iOS上发送的HTTP POST请求]的可能的复制(http://stackoverflow.com/questions/15749486/sending-an-http-post -request-on-ios) –
你在使用REST调用吗? – Arun
[使用Json在Objective C中发布数据]的可能副本(http://stackoverflow.com/questions/9883081/post-data-in-objective-c-using-json) –