質問

this is always error with -1004 ( could not connect to server ) but the request in browser is working fine. what's wrong with my code?

(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);
}];
役に立ちましたか?

解決

You have to set the property baseURL on your request manager. Here is an example:

self.requestManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:http://localhost:3000];

Later, in your concrete GET message to the manager, you'll only pass the relative URL path:

[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);
}];

And keep in mind, that, if you want to send your paramters as JSON, you'll need a request serializer:

self.requestManager.requestSerializer = [AFJSONRequestSerializer serializer];

他のヒント

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);
     }];

You can try this.May be the way you add Parameter is wrong.

Try this method. Hope it helps.

+ (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);

 }];

}

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top