Iphone AFN网络发布到REST Web服务

Iphone AFN网络发布到REST Web服务,iphone,cakephp,rest,post,afnetworking,Iphone,Cakephp,Rest,Post,Afnetworking,简单的背景故事,我们以前的开发人员使用AsitpRequest发出POST请求并从我们的Web服务检索数据。由于未知原因,我们的应用程序的这一部分停止工作。似乎有足够的时间来证明未来,并与AFN合作。RESTWebService在CakePHP框架上运行 简而言之,我没有使用AFNetworking接收请求-响应字符串 我知道Web服务是有效的,因为我能够使用curl成功发布数据并收到正确的响应: curl-d“数据[Model][field0]=field0value和数据[Model][fi

简单的背景故事,我们以前的开发人员使用AsitpRequest发出POST请求并从我们的Web服务检索数据。由于未知原因,我们的应用程序的这一部分停止工作。似乎有足够的时间来证明未来,并与AFN合作。RESTWebService在CakePHP框架上运行

简而言之,我没有使用AFNetworking接收请求-响应字符串

我知道Web服务是有效的,因为我能够使用curl成功发布数据并收到正确的响应: curl-d“数据[Model][field0]=field0value和数据[Model][field1]=field1value”

根据之前开发人员的指示,我提出了以下建议

#import "AFHTTPRequestOperation.h"    

…

- (IBAction)loginButtonPressed {

    NSURL *url = [NSURL URLWithString:@"https://example.com/api/class/function.plist"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"POST"];

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    [request setValue:[usernameTextField text] forHTTPHeaderField:@"data[User][email]"];

    [request setValue:[passwordTextField text] forHTTPHeaderField:@"data[User][password]"];

    AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {  

        NSLog(@"operation hasAcceptableStatusCode: %d", [operation.response statusCode]);

        NSLog(@"response string: %@ ", operation.responseString);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"error: %@", operation.responseString);

    }];

    [operation start];

}
输出: 操作hasAcceptableStatusCode:200 响应字符串:一个空白的plist文件

尝试的解决方案1: 建议的解决方案使用名为operationWithRequest的AFHTTPRequestOperation函数。然而,当我尝试使用上述解决方案时,我得到一个警告“Class method”+operationWithRequest:completion:“not found”(返回类型默认为“id”)

尝试的解决方案2:NSURLConnection。输出:我可以打印发送的成功日志消息,但不能打印响应字符串。 *update-返回空白plist

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *httpBodyData = @"data[User][email]=username@example.com&data[User][password]=awesomepassword";
[httpBodyData dataUsingEncoding:NSUTF8StringEncoding];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[NSData dataWithContentsOfFile:httpBodyData]];
NSHTTPURLResponse __autoreleasing *response;
NSError __autoreleasing *error;
[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];

// *update - returns blank plist
NSData *responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"responseData %@",str);

if (error == nil && response.statusCode == 200) {
    // Process response
    NSLog(@"success");//returns success code of 200 but blank
    NSLog(@"resp %@", response );
} else {
    // Process error
    NSLog(@"error");
}

使用
AFHTTPClient-postPath:parameters:success:failure:
,传递参数(嵌套字典/数组可以)。如果希望返回plist,请确保让客户端注册
afpropertylisterequestoperation

在任何情况下,
setValue:forHTTPHeaderField:
不是您想要的。用于指定有关请求本身的信息;数据是请求正文的一部分。
AFHTTPClient
自动将参数转换为
GET
请求的查询字符串或
POST
等的HTTP正文。

ese是基本的(去掉我为自己使用而设置的条件)行,最终满足了我对web服务的请求。感谢@8vius和@mattt的建议

- (IBAction)loginButtonPressed {        
    NSURL *baseURL = [NSURL URLWithString:@"https://www.example.com/api/class"];

    //build normal NSMutableURLRequest objects
    //make sure to setHTTPMethod to "POST". 
    //from https://github.com/AFNetworking/AFNetworking
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
    [httpClient defaultValueForHeader:@"Accept"];

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            [usernameTextField text], kUsernameField, 
                            [passwordTextField text], kPasswordField, 
                            nil];

    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" 
          path:@"https://www.example.com/api/class/function" parameters:params];

    //Add your request object to an AFHTTPRequestOperation
    AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] 
                                      initWithRequest:request] autorelease];

    //"Why don't I get JSON / XML / Property List in my HTTP client callbacks?"
    //see: https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ
    //mattt's suggestion http://stackoverflow.com/a/9931815/1004227 -
    //-still didn't prevent me from receiving plist data
    //[httpClient registerHTTPOperationClass:
    //         [AFPropertyListParameterEncoding class]];

    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

    [operation setCompletionBlockWithSuccess:
      ^(AFHTTPRequestOperation *operation, 
      id responseObject) {
        NSString *response = [operation responseString];
        NSLog(@"response: [%@]",response);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@", [operation error]);
    }];

    //call start on your request operation
    [operation start];
    [httpClient release];
}

您好,Pouria,您是否尝试使用NSURLConnection发送同步请求?尝试的解决方案2:我是如何尝试NSURLConnection的。您是否正确设置了服务器端的所有内容?例如扩展解析、响应布局和它们的视图?这似乎更像是服务器问题,而不是客户端问题。我最终解决了这个问题。我很抱歉我现在正在工作,稍后将发布更新。我解决了此问题,但我感觉我仍然在做一些错误或效率低下的事情。如果您能快速查看我的解决方案,我将不胜感激。谢谢!注册plist操作只会影响使用
AFHTTPClient-HTTPRequestOperationWithRequest:suc创建的请求操作访问:失败:
alloc init
-ing
AFHTTPRequestOperation
总是会给你一个
AFHTTPRequestOperation
@pouria,这里的路径是什么。它像同一个URL吗。请帮助我,我被路径卡住了。@G.Ganesh我开始有点晚了。路径只是响应HTTP POS的类的一个方法Ts与给定的参数集。我希望您解决了您的问题。