通过Afnetworking从api中获取数据时发生问题

时间:2017-01-27 08:02:23

标签: ios objective-c json api afnetworking

我正在使用iOS Objective-cAFNetworing经理POST JSON数据 这是我的代码,但没有给我数据。

我的代码:

NSString *strUrl2 = @"http://example.extension/admin/services/login.php";

NSMutableDictionary *dataDict = [[NSMutableDictionary alloc] init];
[dataDict setValue:@"dipesh@gmail.com" forKey:@"email"];
[dataDict setValue:@"123456" forKey:@"password"];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [[AFJSONRequestSerializer alloc] init];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json",@"application/x-www-form-urlencoded",@"png/jpg" , nil];

[manager POST:strUrl2
   parameters:dataDict
      success:^(AFHTTPRequestOperation* operation, id responseObject){

          NSLog(@"Response: %@", responseObject);
                   }
      failure:^(AFHTTPRequestOperation* operation, NSError* error){
          NSLog(@"Error: %@", error);
          // Releasing the semaphore to end the process from waiting state.

      }];`
  

没有得到实际回复。

获得回复:

{
  login = {
     message = "Please fill all required parameters";
     status = failure;
  };
}

注意: 这个web服务在postMan和android上工作。

1 个答案:

答案 0 :(得分:2)

   NSString *strUrl2 = @"http://fitgritapp.com/admin/services/login.php";

    NSMutableDictionary *dataDict = [[NSMutableDictionary alloc] init];
    [dataDict setValue:@"dipesh@gmail.com" forKey:@"email"];
    [dataDict setValue:@"123456" forKey:@"password"];


    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:strUrl2 parameters:dataDict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSLog(@"%@",responseObject);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

试试这个

login =     {
        message = "";
        status = success;
        user =         {
            "created_on" = "2017-01-18 12:26:01";
            email = "dipesh@gmail.com";
            "facebook_id" = "";
            "first_name" = Dipesh;
            id = 4;
            "is_active" = 1;
            "last_name" = Sisodiya;
            password = 123456;
            "password_reset_token" = "";
            "updated_on" = "0000-00-00 00:00:00";
        };
    };