Ich habe einen Code in ASIHTTP, aber ich möchte auf AFNetworking weitergehen. ich ASIFormDataRequest für einige POST-Anfragen verwendet und dieser Code funktioniert:ASIFormDataRequest in AFNetworking?
NSURL *url = [NSURL URLWithString:@"http://someapiurl"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"123" forKey:@"phone_number"];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSLog(@"Response: %@", [[request responseString] objectFromJSONString]);
}
aber, als ich versuchte, das gleiche mit AFNetworking zu tun, ich in Problem mit Content-Type bekam (ich glaube).
Dieser Code wird AFNetworking, und es funktioniert nicht:
NSURL *url = [NSURL URLWithString:@"http://dev.url"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"123", @"phone_number",
nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/api/get_archive" parameters:params];
[request setValue:@"application/x-www-form-urlencoded; charset=UTF8" forHTTPHeaderField:@"Content-Type"];
AFHTTPRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest
*request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Response: %@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
NSLog(@"Error: %@", error);
}];
[operation start];
URL in Ordnung ist, wird diese geprüft. Ich bin vom Server immer dieses:
{NSErrorFailingURLKey=http://dev.thisapiurl, NSLocalizedDescription=Expected content type {(
"text/json",
"application/json",
"text/javascript"
)}, got text/html}
Yap, das ist was ich brauchte. Vielen Dank! – dormitkon
Was meinen Sie mit "erwartet einen JSON-freundlichen Antworttyp"? Meine Webdienste geben die JSON-Antwort zurück. Kannst du dir [diese Frage] anschauen (http://stackoverflow.com/questions/14393131/afjsonrequestoperation-returns-null-response-in-ios) – iOSAppDev
@jagill Was meinst du mit "erwartet einen JSON-freundlichen Antworttyp "? Sucht es nach Content-Type = application/json? – iOSAppDev