2016-12-09 30 views
1

您好iam新的iOS開發,我想使用AFHTTPSessionManager發佈一個JSON對象到一個URL。數據無法被讀取,因爲它不是正確的格式 - 目標c

但是,我不斷得到錯誤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]); 
    }]; 

} 
+1

如果使用郵差(或其他類似的解決方案),並把相同的參數,它的工作原理?你可以使用'[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil] encoding:NSUTF8StringEncoding]'在JSON中使用current參數並在POSTMAN中使用它。那它有用嗎? – Larme

+0

您嘗試添加標題。可能會解決你的問題。 –

+0

@Larme tnx。我檢查郵政人,它工作正常。在添加代碼***終止應用程序,由於未捕獲的異常'NSInvalidArgumentException',原因:'*** + [NSJSONSerialization dataWithJSONObject:options:error:]:無效的JSON頂級類型write''我得到這個錯誤。 –

回答

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]; 
+0

TNX。但添加後。我收到了同樣的錯誤。 –

+0

試試這個:http://stackoverflow.com/a/39918680/3901620和http://stackoverflow.com/a/39740394/3901620 – KKRocks

0

刪除行NSLog(@"%@", [error localizedDescription]);。 由[error localizedDescription]返回的字符串不可讀。請參閱Apple NSError文檔。 enter image description here

如果你想看到錯誤信息,使用NSLog(@"%@",error.userInfo[@"NSLocalizedDescription"]);

+0

這不能解決我的問題。 –

相關問題