2014-04-17 3 views
0

AFNetworking lib 버전 2.2.2를 사용 중이며 POST 요청을 사용하여 이미지를 내 서버에 업로드하고 싶습니다. WiFi에서는 모든 것이 매력처럼 작동합니다. 서버는 JSON 응답과 HTTP 200 응답 코드를 반환하므로 서버 측 문제는 발생하지 않습니다.AFNetworking POST 요청이 3G에서 작동하지 않습니다.

3G에서 POST 요청을 사용하면 항상 "시간 초과"예외가 발생하며 그 이유를 알 수 없습니다.

당신이 볼 수 있듯이, 코드는 매우 간단합니다 내 코드

NSMutableDictionary * params = [[NSMutableDictionary alloc] init]; 
if(![self isTextEmpty]) { 
    [params setValue:textView.text forKey:@"t"]; 
} 

[httpOperationManager POST:@"http://......." parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
    if(imageData != nil) { 
     [formData appendPartWithFileData:imageData name:@"p" fileName:@"filename" mimeType:@"image/jpg"]; 
    } 
} success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"Done!!!"); 

    [hud hide:YES]; 

    [self.delegate updateData]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", [error localizedDescription]); 
    [[[UIAlertView alloc] initWithTitle:@"Error sending post!" message:@"Error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; 

    [hud hide:YES]; 
}]; 

입니다. 또한 프로세스 블록을 사용하여 변형을 사용했습니다. 진행 블록을 통해 파일이 완전히 업로드되지만 메서드는 항상 "Timed out"예외를 반환합니다.

업로드 할 파일이 실제로 작아서 현재 200kb 파일로 테스트하고 있지만 성공하지 못한 파일도 테스트했습니다.

나를 도울 수 있기를 바랍니다. Thx 사전에!

답변

0

이것은 나를 위해 일했습니다.

보낼 사전에 데이터 추가.

[registerInputDict setValue:self.fileIdString 
         forKey:@"profileImageId"]; 

AFNetwork 클래스로 사전 보내기.

[super postRequestDataWithLoginURL:@"api/user.json" andRequestName:USER_REGISTER andPostData:userDict]; 


- (AFHTTPRequestOperation *)postRequestDataWithLoginURL:(NSString *)url andRequestName:(NSString *)requestName 
             andPostData:(NSDictionary*)postData { 
[self showProgressIndicator]; 

AFHTTPClient *afHTTPClient = [AFHTTPClient clientWithBaseURL:BASEURL]; 

[afHTTPClient setParameterEncoding:AFJSONParameterEncoding]; 


DebugLog(@"Current Language %@ ",[UserDefaultUtils retriveObjectForKey:LANGUAGE]); 
if ([UserDefaultUtils retriveObjectForKey:LANGUAGE]) { 

    [afHTTPClient setDefaultHeader:@"x-language-code" value:[UserDefaultUtils retriveObjectForKey:LANGUAGE]]; 

} 
NSMutableURLRequest *request = [afHTTPClient requestWithMethod:@"POST" path:url parameters:postData]; 


AFHTTPRequestOperation *operation = [afHTTPClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { 


    [self successWithRequest:operation withRespose:responseObject withRequestName:requestName]; 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

    [self failedWithRequest:operation witherror:error withRequestName:requestName]; 

}]; 

[afHTTPClient enqueueHTTPRequestOperation:operation]; 

return operation; 
} 

성공 및 실패 응답 방법입니다.

- (void)failedWithRequest:(AFHTTPRequestOperation *)operation witherror:(NSError *)error withRequestName:(NSString *)requestName { 

id jsonObject = [operation.responseString objectFromJSONString]; 

DebugLog(@"registration failed:%@",[error description]); 

if ([requestName isEqualToString:UPLOAD_PHOTO]) { 

    if ([self.loginProxyDelegate respondsToSelector:@selector(imageUploadFailed:)]) { 

     [self.loginProxyDelegate imageUploadFailed:[jsonObject objectForKey:@"messages"]]; 
    } 

} 

} 


- (void)successWithRequest:(AFHTTPRequestOperation *)operation withRespose:(id)responseObject withRequestName:(NSString *)requestName { 

id jsonObject = [operation.responseString objectFromJSONString]; 

if ([requestName isEqualToString:UPLOAD_PHOTO]) { 

    [self handleResponseForGetUploadPhoto:jsonObject]; 

} 

} 
+0

빠른 응답을 보내 주셔서 감사합니다. 나는 그것을 시도해 볼 것이지만, 나는 이미 그것을 당신의 방법으로 시도했다고 생각한다 - 같은 결과로. 하지만 당신의 코드를 시험해 보겠습니다. iOS6 또는 iOS7에서 코드를 사용 했습니까? 나는 어딘가에, 애플의 종류가 iOS7에서 네트워크 코드를 망쳤다. 그래서 많은 문제가있다. ... – Nuker

+0

미안하지만, 나는 당신의 코드를 시험해보고 싶었지만 AFNetworking 1.0을 사용하고있는 것처럼 보였다. AFNetworking 2.2.2 .. API가 호환되지 않습니다. – Nuker

관련 문제