这总是错误-1004(无法连接到服务器) 但浏览器中的请求工作正常。 我的代码出了什么问题?
(url : "http://localhost:3000/v1/voice/version?appname=wevoice")
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:@"http://localhost:3000/v1/voice/version"
parameters:@{ @"appname": @"wevoice" }
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
答案 0 :(得分:1)
您必须在请求管理器上设置属性baseURL
。这是一个例子:
self.requestManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:http://localhost:3000];
稍后,在您向经理发送的具体GET消息中,您只会传递相对的URL路径:
[self.requestManager GET:@"/v1/voice/version"
parameters:@{ @"appname": @"wevoice" }
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
请记住,如果您想将您的参数作为JSON发送,那么您需要一个请求序列化程序:
self.requestManager.requestSerializer = [AFJSONRequestSerializer serializer];
答案 1 :(得分:0)
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@"wevoice" forKey:@"appname"];
[manager GET:@"http://localhost:3000/v1/voice/version"
parameters:dic
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
你可以试试这个。可能你添加的方式参数错误。
答案 2 :(得分:0)
试试这个方法。希望它有所帮助。
+ (void)getRequestWithURLStringWithSuccess:(void(^)(id object))successBlock andFail:(void(^)(NSString *errorMessage))failBlock {
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:@"http://localhost:3000/v1/voice/version"]];
NSString *urlString = @"/version";
[manager GET:urlString parameters:@{ @"appname": @"wevoice" } success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}