在Objective C中借助AFNetworking调用Web服务 [英] Calling web services with the help of AFNetworking in Objective C

查看:94
本文介绍了在Objective C中借助AFNetworking调用Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NSURLConnection方法调用Web服务我想要的是用第三方库替换它AFNetworking我该如何实现这个我第一次调用带有数据的Web服务这就是我目前正在做的事情。

I am using NSURLConnection method to call the web services what I want is to replace it with 3rd party library AFNetworking how shall I achieve this I am calling the web Services with data first time here is what I am doing currently.

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL * linkUrl = [NSURL URLWithString:@URLBase];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:linkUrl];
    NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[str length]];

    [theRequest addValue: @"text/plain; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [str dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];


    if( theConnection )
    {
        webData = [[NSMutableData alloc] init];

    }
    else
    {
         NSLog(@"theConnection is NULL");
    }
   }
#pragma mark -- Connection Delegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //NSLog(@"%@",response);
    //[webData setLength: 0];

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

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
     NSLog(@"\nERROR with theConenction");

}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *responseDataString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
    NSLog(@"Converted NSData %@ ",responseDataString);

}

此处

[theRequest setHTTPBody: [str dataUsingEncoding:NSUTF8StringEncoding]];

str是我的加密数据,如用户名和密码。
提前感谢。

str is my encrypted data like username and password. thanks in advance.!

推荐答案

您需要向您的网络服务发送POST请求。您可以使用以下代码段以旧方式。

You need to send POST request to your web services. You can use below code snippet for old way.

NSString *post = [NSString stringWithFormat:@"%@&value=%@&value=%@&value=%@&value=%@&value=%@",self.Comment_Memberid,self.SharedID,self.Post_Ownerid,self.commentText.text,email_stored,password_stored];

    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://.../api/json/postcomment/"]]];
    [request setHTTPMethod:@"POST"];
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postData length]] forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"text/plain; charset=utf-8" forHTTPHeaderField:@"Content-type"];
    //[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
    [request setHTTPBody:postData];
    //get response
    NSHTTPURLResponse* urlResponse = nil;
    NSError *error = [[NSError alloc] init];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"Response Code: %ld", (long)[urlResponse statusCode]);

    if ([urlResponse statusCode] == 200)
    {

    }

如果您想使用AFNetworking发送POST请求,您可以使用以下代码:

And if you want to send POST request using with AFNetworking you can use below code:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *params = @{@"34": x,
                         @"130": y};
[manager POST:@"https://../api/json/cordinates" parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

这篇关于在Objective C中借助AFNetworking调用Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆