2016-12-26 63 views
1

进出口新的IOS我有我想将数据发送到服务器并接收同样的方法响应如何将数据发送到服务器并接收同一methood响应

我有一个类名 profile.m疑问我想将数据发送到服务等级和接收型材类响应

Profile.m:

NSString *parameter = [NSString stringWithFormat:@"%@&access_token=%@&lksite=social&lkmenu=profile&lktype=view&displayedname=%@&displayid=%@", baseUrl, accessToken, userName,userId]; 

Service *service = [[Service alloc]init]; 
    [service sendDataToServer:@"POST" andUrl:parameter andUrl:baseUrl]; 

Service.m

-(void) sendDataToServer:(NSString *) method andUrl:(NSString *) getUrl andUrl:(NSString *)baseUrl{ 
NSMutableData *jsondata; 
Session *ses =[[Session alloc]init]; 
accessToken = [ses getAccessToken]; 
NSLog(@"Access Token---> %@",accessToken); 

NSString *baseUrl1 = [NSString baseUrl]; 


if([method isEqualToString:@"GET"]){ 
    NSString *urlStr = [NSString stringWithFormat: @"%@&access_token=%@",getUrl,accessToken]; 
    url = [NSURL URLWithString: urlStr]; 
    urlRequest= [NSMutableURLRequest requestWithURL:url]; 
    urlRequest.HTTPMethod=method; 
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 
    if(connection) 
    { 
     urlMutable = [NSMutableData new]; 
    } 
}else{ 
    NSData *parameterData = [getUrl dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    url = [NSURL URLWithString: baseUrl1]; 
    urlRequest=[NSMutableURLRequest requestWithURL:url]; 
    urlRequest.HTTPBody=parameterData; 
    urlRequest.HTTPMethod=method; 
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 
    if(connection) 
    { 
     urlMutable = [NSMutableData new]; 
    } 
}} 
+0

只需使用完成处理程序与块,第一个为成功,第二个为失败请求 –

回答

1

您可以使用NSLocalNotification来实现您的任务。 使用NSLocalNotification有三个简单步骤。

1)为此,您可以在Profile.m文件编写代码:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

Service *Service = [[Service alloc] init]; 
     [Service sendDataToServer:(with your parameters here)]; 
} 

-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(responceMethod:) name:@"WebServiceCall" object:nil]; 
} 

2)在Service.m类拨打服务电话

-(void) sendDataToServer:(NSString *) method andUrl:(NSString *) getUrl andUrl:(NSString *)baseUrl{ 
    NSMutableData *jsondata; 
    Session *ses =[[Session alloc]init]; 
    accessToken = [ses getAccessToken]; 
    NSLog(@"Access Token---> %@",accessToken); 

    NSString *baseUrl1 = [NSString baseUrl]; 


    if([method isEqualToString:@"GET"]){ 
     NSString *urlStr = [NSString stringWithFormat: @"%@&access_token=%@",getUrl,accessToken]; 
     url = [NSURL URLWithString: urlStr]; 
     urlRequest= [NSMutableURLRequest requestWithURL:url]; 
     urlRequest.HTTPMethod=method; 
     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 
     if(connection) 
     { 
      urlMutable = [NSMutableData new]; 
     } 
    }else{ 
     NSData *parameterData = [getUrl dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
     url = [NSURL URLWithString: baseUrl1]; 
     urlRequest=[NSMutableURLRequest requestWithURL:url]; 
     urlRequest.HTTPBody=parameterData; 
     urlRequest.HTTPMethod=method; 
     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 
     if(connection) 
     { 
      urlMutable = [NSMutableData new]; 
     } 
    } 

} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 
    int errorCode = (int)httpResponse.statusCode; 
    NSLog(@"response is %d", errorCode); 

    [urlMutable setLength: 0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [urlMutable appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSError *error; 
    id *response = [NSJSONSerialization JSONObjectWithData:webData options:NSJSONReadingAllowFragments error:&error]; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"WebServiceCall" object:nil userInfo:response]; 
} 

最后
3)使用您在Profile.m文件中编写的方法在此实现您的响应解析:

-(void)responceMethod:(NSNotification *)notif{ 
NSDictionary *dict = [notif userInfo]; 
// Your response parsing. 
} 
+0

它必须是评论,不要发布为答案。请阅读相同的文档。 – KAR

+0

@KAR我希望有人可以等待...... !!! –

+0

NSURLConnection? – Abizern

-1

声明块为:

typedef void (^completionBlock)(id responseObject , NSError *error); 

,然后将此块作为添加到您的方法:

- (void) performRequestWithCompletion:(completionBlock)block; 

在调用此方法,将返回“responseObject”和“错误”。错误无成功

相关问题