1

こんにちは、IOS 開発の初心者です。AFHTTPSessionManager を使用して JSON オブジェクトを URL に POST しようとしています。

しかし、私は継続的にエラーが発生していますThe data couldn’t be read because it isn’t in the correct format

誰かがこれを修正するのを手伝ってくれますか? tnx。................................................................... ...................................

方法

- (void) registerUser: (NSString *)emailAdd compnyName:(NSString *) companyNam{

    NSMutableDictionary *userAccount = [[NSMutableDictionary alloc] init];
    [userAccount setObject:@"sighUpForm" forKey:@"$name"];
    [userAccount setObject:@"true" forKey:@"$dirty"];
    [userAccount setObject:@"false" forKey:@"$pristine"];
    [userAccount setObject:@"true" forKey:@"$valid"];
    [userAccount setObject:@"false" forKey:@"$invalid"];
    [userAccount setObject:@"false" forKey:@"$submitted"];
    [userAccount setObject:emailAdd forKey:@"$Email"];
    [userAccount setObject:companyNam forKey:@"$CompanyName"];

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setObject:companyNam forKey:@"HelpdeskDomainName"];
    [dict setObject:companyNam forKey:@"CompanyName"];
    [dict setObject:[NSString stringWithFormat:@"%@ : Admin",companyNam] forKey:@"FirstName"];
    [dict setObject:@"" forKey:@"LastName"];
    [dict setObject:emailAdd forKey:@"SendTo"];
    [dict setObject:@"0" forKey:@"PreferedLanguageID"];
    [dict setObject:@"" forKey:@"AffiliateId"];
    [dict setObject:userAccount forKey:@"UserAccountModel"];
    NSLog(@"signup dicxtionary : %@", dict);

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    NSLog(@"Login dicxtionary : %@", dict);
    MBProgressHUD *hud;
    hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"Please wait....";
    //manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

    [hud showAnimated:YES];
    // send user login data to hosting via AFHTTP Async Request in AFNetworking
    [manager POST:BASEURL parameters:dict progress:nil success:^(NSURLSessionTask *task, id responseObject) {

        [hud hideAnimated:YES];
        // Login response validation
        if (responseObject == [NSNull null]) {

          //  [self showErrorMessage:@"Unable to login. Please try again!" title:@"Login Failed"];
        }else {
            //NSError *error = nil;
            NSLog(@"response type : %@", NSStringFromClass([responseObject class]));
            //NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
            //[self checkResultValue:(NSDictionary *)responseObject];
        }

    } failure:^(NSURLSessionTask *task, NSError *error) {

        NSLog(@"%@", [error localizedDescription]);
    }];

}
4

2 に答える 2

0

これを試して

以下のように変更する必要があります。

 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];


AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setStringEncoding:NSUTF8StringEncoding];

manager.requestSerializer=serializer;
manager.responseSerializer.acceptableContentTypes= [NSSet setWithObjects:@"text/html",@"application/json", nil];
于 2016-12-09T11:06:37.113 に答える
0

行を削除しますNSLog(@"%@", [error localizedDescription]);。によって返される文字列[error localizedDescription]は読み取れません。Apple NSError ドキュメントを参照してください。 ここに画像の説明を入力

エラー情報を見たい場合は、NSLog(@"%@",error.userInfo[@"NSLocalizedDescription"]);

于 2016-12-10T05:38:25.887 に答える