我正在使用AFNetworking lib 2.2.2版,我想使用POST请求将图像上传到我的服务器。在WiFi中,一切都像魅力一样。服务器返回JSON响应以及HTTP 200响应代码,因此不存在服务器端问题。
如果我在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];
}];
如您所见,代码非常简单。我还使用了过程块的变体。使用进度块,我看到文件已完全上传,但该方法始终返回“超时”异常。
我要上传的文件非常小,目前我正在使用200kb文件进行测试,但我也测试了较小的文件没有成功。
我希望你能帮助我! Thx提前!
答案 0 :(得分: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];
}
}