私は要求を送信した時はいつでも私は、私のプロジェクトでAFNetworkingを使用していたが、Imは故障ブロックにこのメッセージを取得し、応答とAFNetworking問題:不正な(401)
エラードメイン= com.alamofire.error.serialization。応答コード= -1011 "要求が失敗しました:不正な(401)" のUserInfo = {com.alamofire.serialization.response.error.response = com.alamofire.serialization.response.error.data = < 7b227375 63636573 73223a66 616c7365 2c226d65 73736167 65223a22 41757468 656e7469 63617469 6f6e2066 61696c65 64227d>、NSLocalizedDescription =リクエスト 失敗:不正(401)}
これは私がAFNetworkingを使用してリクエストを送信する方法です。
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL]];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", nil];
NSString *path = [NSString stringWithFormat:@"/api/##/##/##/sign_in"];
NSString *params = @"{\"user\": {\"uid\": \"10236412002551\",\"access_token\":\"g6tdbvc34seadcx7yufbbcgvf8ijhss\",\"email\": \"[email protected]\",\"latitude\": \"6.927079\", \"longitude\": \"79.861243\",\"remote_avatar_url\": \"\"}}";
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
AFSecurityPolicy* policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
[policy setValidatesDomainName:NO];
[policy setAllowInvalidCertificates:YES];
[manager POST:path parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
if([[responseObject objectForKey:@"error_code"] intValue]==0){
if (success) {
success(userArray);
}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
しかし、私はNSURLSessionを使用して、正しい応答を得ることができるようになります。(コードの下に参照してください)
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http://##/api/##/##/##/sign_in"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params [email protected]"{\"user\": {\"uid\": \"10236412002551\",\"access_token\":\"g6tdbvc34seadcx7yufbbcgvf8ijhss\",\"email\": \"[email protected]\",\"latitude\": 6.927079, \"longitude\": 79.861243,\"remote_avatar_url\": \"\"}}";
[urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response:%@ %@\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSDictionary * json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"Data = %@",text);
NSLog(@"Data = %@",json);
}
}];
[dataTask resume];
私はAFNetworkingを使用してに興味がある、しかし、この問題が原因で私が勝ちました前方に進むことができます。 AFNetworkingの問題を解決するのを手伝ってください。
これは私が送るべき要求である、
{
"user": {
"uid": "102364120025511",
"access_token": "g6tdbvc34seadcx7yufbbcgaavf8ijh",
"email": "[email protected]",
"latitude": 6.927079,
"longitude": 79.861243,
"remote_avatar_url": ""
}
}
これは、あなたがJSON文字列としてあなたparams
を提供して、使用するAFNetworkingを指示している、
{
"success": true,
"message": "Successfully authenticated",
"user": {
"id": 123,
"email": "[email protected]",
"provider": "facebook",
"latitude": 6.927079,
"longitude": 79.861243,
"avatar": {
"url": "",
"thumb": ""
}
}
}
「7b227375 ...」データに含まれているものが不思議であれば、それは '{" success ":false、" message ":" Authentication failed "}'というJSONペイロードです。 – Rob