2

My problem is that I have a old code and I dont know how to change it. I had 1 class called API (AFHTTPClient) I have problems with 2 methods because I dont know how to put them in 2.0: This:

-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock
{
NSMutableURLRequest *apiRequest = 
[self multipartFormRequestWithMethod:@"POST" 
path:kAPIPath 
parameters:params 
constructingBodyWithBlock: ^(id formData) {
//TODO: attach file if needed
}];

AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    //success!
    completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failure :(
    completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];

[operation start];
}


and this:


#pragma mark - init
//intialize the API class with the destination host name

-(API*)init
{
//call super init
self = [super init];

if (self != nil) {
    //initialize the object
    user = nil;

    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];

    // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
    [self setDefaultHeader:@"Accept" value:@"application/json"];
}

return self;
}

I do a new class called Api, now (AFHTTPRequestOperationManager) is good? I try with this, but I dont have idea

-(API*)init
{//call super init
self = [super init];

//initialize the object
if (self != nil) {
    //initialize the object
    user = nil;
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    AFJSONRequestSerializer *a=[AFJSONRequestSerializer serializer];


    [a setValue:@"Accept" forHTTPHeaderField:@"application/json"];
    [a setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [a setValue:@"content-Type" forHTTPHeaderField:@"text/html"];
    [a setValue : @ "text / html; charset = UTF-8" forHTTPHeaderField : @ "Content-Type" ];

}
return self;
}

-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock
{

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = params;
NSURL *filePath = [NSURL URLWithString:@"http://162.243.199.147/mujeresquecorren/iReporter/index.php"];
[manager POST:@"api" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  [formData appendPartWithFileURL:filePath name:@"api" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
 NSMutableURLRequest *apiRequest = [NSMutableURLRequest requestWithURL:filePath];
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
// operationManagerInstance.requestSerializer = requestSerializer;
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:apiRequest];
operation.responseSerializer = [AFJSONResponseSerializer serializer];

manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"http://162.243.199.147/mujeresquecorren/iReporter/index.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@", responseObject);
} failure:nil];
// AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//success!
completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//failure :(
completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];

[operation start];
}

and this is one of the errors:

rror: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0xa64f980 {NSErrorFailingURLStringKey=api, NSErrorFailingURLKey=api, NSLocalizedDescription=unsupported URL, NSUnderlyingError=0xa782e10 "unsupported URL"}

Please help, im going to be crazy with that and I need that my code works in my app! Thanks a lot!!!!

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
natijauskas
  • 183
  • 1
  • 3
  • 12
  • what is the problem with the original `AFNetworking`'s migration guide...? have you read the documentation for v2.0 of `AFNetworking` yet? – holex Dec 27 '13 at 00:10
  • Hi holex, yeah I have read the guide but I dont know how to do this – natijauskas Dec 27 '13 at 18:15
  • I have also read the guide and it doesn't help when I already have AFNetworking 1.0 code for AFHTTPClient similar to @natjauskas. There needs to be better examples added to the migration guide. – motionpotion Sep 28 '14 at 11:53

1 Answers1

0

It's all there in the error message. "api" isn't a valid URL. You need to either specify an absolute URL, or initialize your request operation manager with a baseURL, using initWithBaseURL:.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Hi @AaronBrager! I do that, and my error has changed. Now is:Expected status code(200-299)got 500 !!:(!Any idea? – natijauskas Dec 27 '13 at 19:37
  • Your server is rejecting your post for some reason. Most likely you need to provide some additional information that you're not providing, for example an access token or a content-type header. – Aaron Brager Dec 27 '13 at 21:06
  • Anyway, this error should be posted as a separate question, with as much additional information as possible, since it is a separate problem. – Aaron Brager Dec 27 '13 at 21:07
  • ok!!Its true!!Sorry!!I write an other post with the new problem!:-) – natijauskas Dec 27 '13 at 23:55
  • I dont why,because I have change something but with your answer my code works right now!! Thanks Aaron! – natijauskas Dec 30 '13 at 00:46