Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS使用JSON参数从AFHTTP POST方法获取JSON响应_Ios_Iphone_Json_Post_Afnetworking 2 - Fatal编程技术网

iOS使用JSON参数从AFHTTP POST方法获取JSON响应

iOS使用JSON参数从AFHTTP POST方法获取JSON响应,ios,iphone,json,post,afnetworking-2,Ios,Iphone,Json,Post,Afnetworking 2,在JSON参数发布时,我收到一个404错误https://api.hackerearth.com/codemonk/v1/topic…详细信息/。服务器使用POST HTTP方法获取主题的详细信息,成功后将得到JSON响应。POST参数是topic对象的id。POST参数应为JSON格式。 我正在使用AFN网络,如下所示- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manag

JSON参数发布时,我收到一个
404
错误https://api.hackerearth.com/codemonk/v1/topic…详细信息/
。服务器使用POST HTTP方法获取
主题
的详细信息,成功后将得到JSON响应。POST参数是
topic
对象的
id
。POST参数应为JSON格式。 我正在使用AFN网络,如下所示-

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];

NSDictionary *params = [NSDictionary dictionaryWithObject:@1 forKey:@"id"];

NSString *str = @"https://api.hackerearth.com/codemonk/v1/topic­-detail/";

NSString *encodedStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[manager POST:encodedStr
   parameters:params
      success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {

    NSLog(@"responseObject : %@",responseObject);


} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {

    NSLog(@"error : %@", error.localizedDescription);

}];
这是常规的东西,但我不知道为什么我现在不能把它弄对。我只收到一个
404页面未找到
错误。这肯定不是服务器端问题。有人帮忙吗?

也许这会有帮助

AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

operationManagerInstance.requestSerializer = requestSerializer;
================更新

我有404当复制你的网址。这是因为主题细节之间的连字符符号实际上不是连字符。这是一个不起作用的特殊角色

-细节/


相反,我删除了它并手动键入连字符,它工作正常。

删除以下行

NSString *encodedStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
请尝试以下操作:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];

NSDictionary *params = [NSDictionary dictionaryWithObject:@1 forKey:@"id"];

NSString *str = @"https://api.hackerearth.com/codemonk/v1/topic­-detail/";

[manager POST:str
   parameters:params
      success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {

    NSLog(@"responseObject : %@",responseObject);


} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {

    NSLog(@"error : %@", error.localizedDescription);

}];

您将id值设置为1,表示该链接;这个链接是404。我遗漏了什么吗?这是一个POST请求,我需要将{“id”:1}作为json参数体传递给这个POST请求。使用作为体提供的json dict发送POST请求也会产生404错误。你能提供文件吗?也许API需要一些标题你确定你使用的链接正确吗?这似乎是服务器端的问题。我试着和邮递员一起运行你的请求,结果也是一样的。文档非常标准,这里是链接:已经尝试了这个显式json设置,再次尝试,但仍然得到了一个
404
错误上帝的爱,是的,就是这样,连字符是罪魁祸首-这就是为什么没有一个帖子请求不起作用的原因(所有的帖子都有连字符)谢谢你的朋友:)你的问题让我很开心!这比仅仅在请求后的正文中发送JSON有趣得多:)