【问题标题】:Calling web services with the help of AFNetworking in Objective C借助 Objective C 中的 AFNetworking 调用 Web 服务
【发布时间】:2017-07-23 05:56:36
【问题描述】:

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

- (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 是我的加密数据,例如用户名和密码。 提前谢谢!

【问题讨论】:

    标签: ios objective-c afnetworking


    【解决方案1】:

    您需要向您的网络服务发送 POST 请求。您可以将以下代码 sn-p 用于旧方式。

    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 请求,您可以使用以下代码:

    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);
    }];
    

    【讨论】:

    • 我的数据是简单的TextPlain
    • 没关系。您应该使用上述代码将数据传递给 Web 服务。您的数据可以是图像(NSData)或只是一个纯字符串。您可以使用此代码部分。
    猜你喜欢
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多