简体   繁体   English

怎么把NSURLConnection转换成AFNetworking?

[英]How to convert NSURLConnection to AFNetworking?

i want to convert this code to AFNetworking but i have a error. 我想将此代码转换为AFNetworking,但出现错误。 i used 我用了

AFNetworking POST to REST webservice this code. AFNetworking POST到REST Web服务的此代码。

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

NSString *latest_url = @"url_string";

[request setURL:[NSURL URLWithString:latest_url]];  

[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:useragent_string forHTTPHeaderField:@"User-Agent"];
[request setValue:host_string forHTTPHeaderField:@"Host"];
[request setValue:@"keep-alive" forHTTPHeaderField:@"Connection"];
[request setValue:@"keep-alive" forHTTPHeaderField:@"Proxy-Connection"];
[request setTimeoutInterval:30.0];
[request setHTTPBody:postData];   

NSError *errorx = nil;

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&errorx];

NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

NSData *jsonData = [json_string dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonerror;

NSData *get_data_from_request = [ourdelegate do_request:request_url post_array:request_post_array debug:istekdebug];

NSArray *statuses =[NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &jsonerror];

How to convert this code to AFNetworking? 如何将此代码转换为AFNetworking?

Since you aren't being specific with the error like rckoenes mentioned above....... 由于您并不是特定于上面提到的rckoenes之类的错误.......

Why don't you just go get PAW from LuckyMarmot 您为什么不直接从LuckyMarmot获取PAW

It helps you formulate REST api calls and will translate the request into AFNetworking for you. 它可以帮助您制定REST api调用,并将请求转换为AFNetworking。 Phenomenal tool for only $19.99. 现象工具仅售$ 19.99。 Worth every penny. 值得每一分钱。

Since you are using post request, here's what you can do with AFHTTPSessionManager. 由于您使用的是发布请求,因此可以使用AFHTTPSessionManager执行以下操作。 You can also call AFHTTPSessionManager Get method with block invocation. 您还可以通过块调用来调用AFHTTPSessionManager的Get方法。

NSURL *baseURL = [NSURL URLWithString:BaseURLString];
NSDictionary *parameters = @{@"Host": host_string};

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];


[manager POST:@"yourFile.php" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
         NSLog("handle succes");
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
         NSLog("handle error %@",[error localizedDescription]);
}];

Have fun :) 玩得开心 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM