Tóm tắt cốt truyện, nhà phát triển trước đây của chúng tôi đã sử dụng ASIHTTPRequest để thực hiện yêu cầu POST và truy xuất dữ liệu từ dịch vụ web của chúng tôi. Vì lý do không rõ phần này của ứng dụng của chúng tôi đã ngừng hoạt động. Dường như thời gian đủ tốt để chứng minh trong tương lai và đi với AFNetworking. REST webservice chạy trên khung công tác CakePHP.AFNetworking POST tới REST webservice
Tóm lại, tôi không nhận được chuỗi phản hồi yêu cầu bằng cách sử dụng AFNetworking.
Tôi biết webservice hoạt động vì tôi có thể đăng dữ liệu thành công và nhận được phản hồi thích hợp bằng cách sử dụng curl: curl -d "dữ liệu [Model] [field0] = field0value & data [Model] [field1] = field1value" https://example.com/api/class/function.plist
Theo hướng dẫn của nhà phát triển trước, tôi đã đưa ra những điều sau đây.
#import "AFHTTPRequestOperation.h"
…
- (IBAction)loginButtonPressed {
NSURL *url = [NSURL URLWithString:@"https://example.com/api/class/function.plist"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[usernameTextField text] forHTTPHeaderField:@"data[User][email]"];
[request setValue:[passwordTextField text] forHTTPHeaderField:@"data[User][password]"];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation hasAcceptableStatusCode: %d", [operation.response statusCode]);
NSLog(@"response string: %@ ", operation.responseString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", operation.responseString);
}];
[operation start];
}
đầu ra: hoạt động hasAcceptableStatusCode: 200 phản ứng chuỗi: một plist tập tin trống
cố gắng giải pháp 1: AFNetworking Post Request các giải pháp đề xuất sử dụng một chức năng của AFHTTPRequestOperation gọi operationWithRequest. Tuy nhiên, khi tôi cố gắng sử dụng giải pháp đã nói, tôi nhận được cảnh báo "Phương thức lớp '+ operationWithRequest: completion:' không tìm thấy (kiểu trả về mặc định là 'id'"
giải pháp đã thử 2: Kết nối NSURLConnection. in nhật ký thành công nhắn tin cho nhưng không phải là chuỗi phản ứng * cập nhật -.. lợi nhuận plist trống
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *httpBodyData = @"data[User][email][email protected]&data[User][password]=awesomepassword";
[httpBodyData dataUsingEncoding:NSUTF8StringEncoding];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[NSData dataWithContentsOfFile:httpBodyData]];
NSHTTPURLResponse __autoreleasing *response;
NSError __autoreleasing *error;
[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
// *update - returns blank plist
NSData *responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"responseData %@",str);
if (error == nil && response.statusCode == 200) {
// Process response
NSLog(@"success");//returns success code of 200 but blank
NSLog(@"resp %@", response);
} else {
// Process error
NSLog(@"error");
}
Xin chào, Pouria, bạn đã thử sử dụng NSURLConnection để gửi yêu cầu đồng bộ chưa? – 8vius
giải pháp đã thử 2: là cách tôi đã thử NSURLConnection. – Airuop
Bạn có mọi thứ ở phía máy chủ được thiết lập đúng không? Giống như phân tích cú pháp tiện ích mở rộng, bố cục cho phản hồi và lượt xem cho chúng? Có vẻ như vấn đề máy chủ nhiều hơn so với máy khách. – 8vius