dongyue934001 2014-10-16 18:16
浏览 116
已采纳

AfNetworking 2.0发布问题

i have recently wanted to use Afnetworking in my ios application. the same page responds me by using ASIHTTPREQUEST. but it simply does not with the AFNetworking. Here is my effort.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"user[height]": @"10",
                             @"user[weight]": @"255"};
    [manager POST:@"http://localhost:8888/TestingPost/post.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

Now i have tried many options like adding serialization to different xml / json / html. but of no use.

On PHP page i am not doing something fancy just printing out whatever is posted on the page. still here it is.

<?php
// Show all information, defaults to INFO_ALL

header('Content-type: application/JSON');



print_r($_POST[]);
/*
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$password = $_POST['password'];
*/

//echo( "hurray");

?>

Can you please shed some light on it. i want to switch from ASIhttprequest to newest and more supported.

Here is the result for the request

Networking_example[1777:41979] Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: internal server error (500)" UserInfo=0x7f9feae16d60 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7f9fedb029a0> { URL: http://localhost:8888/TestingPost/post.php } { status code: 500, headers {
    Connection = close;
    "Content-Length" = 0;
    "Content-Type" = "text/html";
    Date = "Thu, 16 Oct 2014 18:08:08 GMT";
    Server = Apache;
    "X-Powered-By" = "PHP/5.5.10";
} }, NSErrorFailingURLKey=http://localhost:8888/TestingPost/post.php, com.alamofire.serialization.response.error.data=<>, NSLocalizedDescription=Request failed: internal server error (500)}
  • 写回答

1条回答 默认 最新

  • duan3019 2014-10-16 19:54
    关注

    If your PHP is using $_POST, that means that you are ok using the default requestSerializer of AFHTTPRequestSerializer (i.e. an application/x-www-form-urlencoded request).

    But your JSON is not generation JSON response, so you have to change the responseSerializer to AFHTTPResponseSerializer.

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    

    And your PHP page is reporting a 500 error. That is an "Internal Server Error", for which the status code definition says:

    The server encountered an unexpected condition which prevented it from fulfilling the request.

    That suggests a PHP error. Perhaps you intended:

    print_r($_POST);
    

    Generally, when interacting with web service, it's better to generate JSON response, e.g.

    echo(json_encode($_POST));
    

    Obviously, if you generate JSON response, you might also want to use the default [AFJSONResponseSerializer serializer] setting, rather than changing it to AFHTTPResponseSerializer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目