gpt4 book ai didi

ios - 从 nsurlconnection 委托(delegate)方法返回响应数据

转载 作者:行者123 更新时间:2023-11-28 21:49:03 25 4
gpt4 key购买 nike

您好,我正在使用网络服务进行注册,我创建了一个单独的类来访问网络服务并在 View 类上返回响应。

这是我的代码 在按钮上

-(IBAction)placeOrder:(id)sender
{
url = [[NSURL alloc]initWithString:@"http://54.25fdg.239.126/tfl/index.php/service/register"];
NSString *str = [NSString stringWithFormat:@"name=%@&phoneNumber=%@&emailAddress=%@&password=%@&addressLine1=%@&addressLine2=%@&city=%@&pincode=%@&landmark=%@&specialInstruction=%@",txtUserName.text,txtPhone.text,txtEmail.text,txtPassword.text,addressLine1Tf.text,addressLine2Tf.text,cityTf.text,pinCodeTf.text,landMarkTf.text,specialInstrTv.text];
WebServices *webservice = [[WebServices alloc]init];
[webservice getDataFromService:url data:str];
responseDictionary = [webservice returnResponseData];
}

在调用 getDataFromService 方法后,它会调用 returnRespnoseData 方法。然后它调用从 getDataFromService 调用的连接方法。所以我从 returnResponseData 得到了 nil 响应。任何人都可以告诉我如何管理它或如何将 didfinishloading 方法的响应返回到主视图类?在 webservice.m

-(void)getDataFromService:(NSURL *)url data:(NSString *)parameterString
{
NSData *postData = [parameterString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[conn start];
}

在连接的最后一个方法中,我在字典中得到了作为 jsonDec 的响应。

 -(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError* error ;
_jsonDec = [NSJSONSerialization JSONObjectWithData:_responseData options:NSJSONReadingAllowFragments error:&error];
}

将 jsonDec 返回给 View 类

-(NSDictionary *)returnResponseData
{
return _jsonDec;
}

最佳答案

为确保应用程序在响应到达后将 jsondata 返回给 viewclass,请在您的类的 .h 文件中定义一个 block :

typedef void(^getDataBlock)(id jsonObject);

那么您的 getDataFromService 方法可能如下所示:

-(void)getDataFromService:(NSURL *)url data:(NSString *)parameterString getDataBlock:(getDataBlock)returnBlock{
NSLog(@"Getting Data");
NSData *postData = [parameterString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSError *error = [[NSError alloc] init];
id jsonDec = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
returnBlock(jsonDec);
}];
}

在您的 View 类中,您现在可以这样做:

-(void)test{
//Insert the right name for your class where you do the request and insert the right values
[backendClass getDataFromService:[NSURL URLWithString:@"www.google.de"] data:@"blablabla" getDataBlock:^(id jsonObject) {
//Make sure you're on the mainthread for UI-updates
dispatch_async(dispatch_get_main_queue(), ^{
self.titleLabel.text = [jsonObject objectForKey:@"title"];
});
}];
}

关于ios - 从 nsurlconnection 委托(delegate)方法返回响应数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29007881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com