Objective c AFN网络中的ASIFormDataRequest?
我有一些ASITP代码,但我想继续使用AFHTTP。 我在一些POST请求中使用了ASIFormDataRequest,该代码运行良好:Objective c AFN网络中的ASIFormDataRequest?,objective-c,ios,asihttprequest,afnetworking,Objective C,Ios,Asihttprequest,Afnetworking,我有一些ASITP代码,但我想继续使用AFHTTP。 我在一些POST请求中使用了ASIFormDataRequest,该代码运行良好: NSURL *url = [NSURL URLWithString:@"http://someapiurl"]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setPostValue:@"123" forKey:@"phone_number"];
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]);
}
但是,当我试图用AFNetworking做同样的事情时,我发现内容类型有问题(我猜)
这是一段网络代码,但不起作用:
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是好的,这是检查。
我从服务器上获得以下信息:
{NSErrorFailingURLKey=http://dev.thisapiurl, NSLocalizedDescription=Expected content type {(
"text/json",
"application/json",
"text/javascript"
)}, got text/html}
我最近和你经历了同样的事情。这是我编写的一个自定义类,它可以处理几乎所有的网络请求 NetworkClient.h:
//
// NetworkClient.h
//
// Created by LJ Wilson on 3/8/12.
// Copyright (c) 2012 LJ Wilson. All rights reserved.
//
#import <Foundation/Foundation.h>
extern NSString * const ACHAPIKey;
@interface NetworkClient : NSObject
+(void)processURLRequestWithURL:(NSString *)url
andParams:(NSDictionary *)params
block:(void (^)(id obj))block;
+(void)processURLRequestWithURL:(NSString *)url
andParams:(NSDictionary *)params
syncRequest:(BOOL)syncRequest
block:(void (^)(id obj))block;
+(void)processURLRequestWithURL:(NSString *)url
andParams:(NSDictionary *)params
syncRequest:(BOOL)syncRequest
alertUserOnFailure:(BOOL)alertUserOnFailure
block:(void (^)(id obj))block;
+(void)handleNetworkErrorWithError:(NSError *)error;
+(void)handleNoAccessWithReason:(NSString *)reason;
@end
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"ParamValue1", @"ParamName1",
@"ParamValue2", @"ParamName2",
nil];
[NetworkClient processURLRequestWithURL:nil andParams:params block:^(id obj) {
if ([obj isKindOfClass:[NSArray class]]) {
// Do whatever you want with the object. In this case, I knew I was expecting an Array, but it will return a Dictionary if that is what the web-service responds with.
}
}];
这增加了一些您可能不需要或不想要的功能,只要版权部分保持不变,您可以根据需要随意修改。我使用那个APIKey来验证来自我的应用程序的请求,而不是试图破解的人
调用它(假设已包含NetworkClient.h:
//
// NetworkClient.h
//
// Created by LJ Wilson on 3/8/12.
// Copyright (c) 2012 LJ Wilson. All rights reserved.
//
#import <Foundation/Foundation.h>
extern NSString * const ACHAPIKey;
@interface NetworkClient : NSObject
+(void)processURLRequestWithURL:(NSString *)url
andParams:(NSDictionary *)params
block:(void (^)(id obj))block;
+(void)processURLRequestWithURL:(NSString *)url
andParams:(NSDictionary *)params
syncRequest:(BOOL)syncRequest
block:(void (^)(id obj))block;
+(void)processURLRequestWithURL:(NSString *)url
andParams:(NSDictionary *)params
syncRequest:(BOOL)syncRequest
alertUserOnFailure:(BOOL)alertUserOnFailure
block:(void (^)(id obj))block;
+(void)handleNetworkErrorWithError:(NSError *)error;
+(void)handleNoAccessWithReason:(NSString *)reason;
@end
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"ParamValue1", @"ParamName1",
@"ParamValue2", @"ParamName2",
nil];
[NetworkClient processURLRequestWithURL:nil andParams:params block:^(id obj) {
if ([obj isKindOfClass:[NSArray class]]) {
// Do whatever you want with the object. In this case, I knew I was expecting an Array, but it will return a Dictionary if that is what the web-service responds with.
}
}];
还可以:
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"ParamValue1", @"ParamName1",
nil];
NSString *urlString = @"https://SuppliedURLOverridesDefault";
[NetworkClient processURLRequestWithURL:urlString
andParams:params
syncRequest:YES
alertUserOnFailure:NO
block:^(id obj) {
if ([obj isKindOfClass:[NSArray class]]) {
// Do stuff
}
}];
因此,它将接受任意数量的参数,如果您愿意,可以插入APIKey或其他任何东西,然后根据web服务返回字典或数组。这需要SBJson BTW。您遇到的问题是因为您正在实例化AFJSONRequestOperation,默认情况下,它需要JSON友好的响应类型。是否需要JSON响应?如果不是,则应使用不太特定的请求类。例如,可以使用HTTPRequestOperationWithRequest:
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"];
//Notice the different method here!
AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"Error: %@", error);
}];
//Enqueue it instead of just starting it.
[httpClient enqueueHTTPRequestOperation:operation];
如果您有更具体的请求/响应类型(JSON、XML等),您可以使用这些特定的AFHTTPRequestOperation子类。否则,只需使用普通的HTTP子类。EIJay,thx作为您的答案,但我想使用AFNetworking framework,因为我知道它会得到维护并变得更好。这就是我离开Asitp的原因我离开ASI的原因是因为AFNetworking使用了块In不要使用委托方法,因为如果不重写.Yap,它永远不会与ARC兼容,这就是我所需要的。谢谢!你说的“期望JSON友好响应类型”是什么意思?我的web服务返回JSON响应。你能看看@jagill吗?你说的“期望JSON友好响应类型”是什么意思?是否检查内容类型=应用程序/json?