我使用最新版本的AFNetworking
来查询我的服务器。
我的服务器端PHP正在发送与PHP相关的错误但我无法看到错误是什么(所以我可以调试它),因为AFNetworking期待一个JSON。
有人能告诉我怎样才能看到完整的HTTP结果。我已经研究过,而且我知道operation.responseString
/ success
块中的fail
与我当前的块没有AFHTTPRequestOperation* operation
有关,我无法将其添加为另外一个块变量。
我真的很感激一些帮助。
以下代码使用
NSURL *url = [[NSURL alloc]initWithString:URL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:URLPATH parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
//...
}
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
NSLog(@"Inside the success block %@",JSON);
[[self delegate ]shareExerciseDone:JSON];
[[self delegate] activityIndicatorFinish];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
NSLog(@"Response text is: %@", operation.responseString);
NSLog(@"json text is: %@", JSON);
NSLog(@"Request failed with error: %@, %@", error, error.userInfo);
[[self delegate] activityIndicatorFinish];
}];
[operation start];
错误
Request failed with error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0xc1b4520 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}, {
NSDebugDescription = "JSON text did not start with array or object and option to allow fragments not set.";
答案 0 :(得分:1)
将图像从iOS上传到服务器我正在使用Afnetworking
您可以尝试使用此代码吗?您将获得成功或失败的回复。
NSData *userImgToUpload = UIImagePNGRepresentation(userImg.image);
NSURL *url = [NSURL URLWithString:@"www.domainname.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSLog(@"%@",@"Uploading data");
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:usernameTxt.text,@"username", firstnameTxt.text,@"first_name",lastnameTxt.text,@"last_name", nil];
// Upload Image
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/addPatient" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:userImgToUpload name:@"patientimage" fileName:[NSString stringWithFormat:@"%@.png",usernameTxt.text] mimeType:@"image/png"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"response: %@",[operation.responseString objectFromJSONString]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Upload Failed");
NSLog(@"error: %@", [operation error]);
}];
[operation start];
答案 1 :(得分:1)
请勿使用AFJSONRequestOperation
,只需使用AFHTTPRequestOperation
。
答案 2 :(得分:0)
您应该听取在操作开始和结束时触发的AFNetworkingOperationDidStartNotification
和AFNetworkingOperationDidFinishNotification
事件:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(YourOperationStartMethod:)
name:AFNetworkingOperationDidStartNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(YourOperationFinishMethod:)
name:AFNetworkingOperationDidFinishNotification
object:nil];
您可以从通知中获取操作对象,如下所示:
AFHTTPRequestOperation *operation =
(AFHTTPRequestOperation *) [notification object];
此operation
对象包含request
和response
属性。
所有这些工作原理的一个很好的例子可以在AFHTTPRequestOperationLogger插件中找到(当然,你可以简单地使用它,而不是编写你自己的东西)。