使用 AFNetworking 的 API 请求出错

Posted

技术标签:

【中文标题】使用 AFNetworking 的 API 请求出错【英文标题】:Error in API request using AFNetworking 【发布时间】:2017-08-08 03:27:37 【问题描述】:

我为用户调用更新 api 我的参数是混合语言。参数是这样的:


    Address = "Judd ";
    AnnualIncome = 0;
    AreYouManglik = NO;
    AreYouPhysicallyChallenged = NO;
    MaritalStatus = "Married/\U0935\U093f\U0935\U093e\U0939\U093f\U0924";
    Mobile = 8441960471;
    MotherGotra = "Not Available";
    MotherProfession = "Not Available";


注意 MaritalStatus = "Married/\U0935\U093f\U0935\U093e\U0939\U093f\U0924" 就像:"Married/विवाहित"

在邮递员上它工作正常,但在我的代码中给出错误:

request failed >>>>  :Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo=NSDebugDescription=Invalid value around character 0., NSUnderlyingError=0x60000045b630 Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: internal server error (500)" UserInfo=

com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x60800063c300>  URL: http://bagrasamaaj.com/myapp/api/updateprofile   status code: 500, headers 
    "Cache-Control" = "no-cache, private";
    Connection = close;
    "Content-Encoding" = gzip;
    "Content-Type" = "text/html; charset=UTF-8";
    Date = "Tue, 08 Aug 2017 03:11:00 GMT";
    Server = Apache;
    "Transfer-Encoding" = Identity;
    Vary = "Accept-Encoding,User-Agent";
    "X-Powered-By" = "php/5.6.30";
    "X-RateLimit-Limit" = 60;
    "X-RateLimit-Remaining" = 59;
 , NSErrorFailingURLKey=http://bagrasamaaj.com/myapp/api/updateprofile, com.alamofire.serialization.response.error.data=<3c21444f 43545950 45206874 6d6c3e0a 3c68746d 6c3e0a20 2020203c 68656164 3e0a2020 20202020 20203c6d 65746120 63686172 7365743d 22555446 2d382220 2f3e0a20 20202020 2020203c 6d657461 206e616d 653d2272 6f626f74 73222063 6f6e7465 6e743d22 6e6f696e 6465782c 6e6f666f 6c6c6f77 22202f3e 0a202020 20202020 203c7374 796c653e 20202020 20202020 20202020 626f6479 207b2062 61636b67 726f756e 642d636f 6c6f723a 20234639 46394639 3b20636f 6c6f723a 20233232 323b2066 6f6e743a 20313470 782f312e 34204865 6c766574 6963612c 20417269 616c2c20 73616e73 2d736572 69663b20 6d617267 696e3a20 303b2070 61646469 6e672d62 6f74746f 6d3a2034 3570783b 207d0a0a 20202020 20202020 20202020 61207b20 63757273 6f723a20 706f696e 7465723b 20746578 742d6465 636f7261 74696f6e 3a206e6f 6e653b20 7d0a2020 20202020 20202020 2020613a 686f7665 72207b20 74657874 2d646563 6f726174 696f6e3a 20756e64 65726c69 6e653b20 7d0a2020 20202020 20202020 20206162 62725b74 69746c65 5d207b20 626f7264 65722d62 6f74746f 6d3a206e 6f6e653b 20637572 736f723a 2068656c 703b2074 6578742d 6465636f 72617469 6f6e3a20 6e6f6e65 3b207d0a 0a202020 20202020 20202020 20636f64 652c2070 7265207b 20666f6e 743a2031 3370782f 312e3520 436f6e73 6 ......

API 代码:

  AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    manager.responseSerializer = responseSerializer;

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];

    NSLog(@"dict update %@",dict);

    NSString*url = [NSString stringWithFormat:@"%@updateprofile",baseUrl];

    [manager POST:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) 

        if(profilePic !=nil)
            NSData *imageData = UIImageJPEGRepresentation(profilePic, 1.0);

            [formData appendPartWithFileData:imageData name:@"image" fileName:@"image" mimeType:@"image/jpeg"];
        

     progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) 

        NSLog(@"response update: %@",responseObject);
        [delegate passRequestedArray:responseObject WithId:@"update"];


     failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) 

        NSLog(@"request failed >>>>  :%@",error);

     ];

提前致谢。

【问题讨论】:

"Invalid value around character 0." 响应不是 JSON。您能否删除AFJSONResponseSerializer 并将其设置为“基本”,我认为它是AFResponseSerializer,然后在响应中记录responseObject(如果它属于NSData,您可以将其翻译成NSString( initWithData:encoding:)) 并给我们价值? 此外,还有一个 HTTP 500,该消息以 &lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt;&lt;meta charset="UTF-8" /&gt;&lt;meta name="robots" content="noindex,nofollow" /&gt;&lt;style&gt;body background-color: #F9F9F9; color: #222; font: 14px/1.4 Helvetica, Arial, sans-serif; margin: 0; padding-bottom: 45px; a cursor: pointer; text-decoration: none; a:hover text-decoration: underline; abbr[title] border-bottom: none; cursor: help; text-decoration: none; code, pre font: 13px/1.5 Cons 开头(但您没有提供完整的消息)。先修复那个错误。比如baseUrl/作为后缀吗? 【参考方案1】:

我认为错误是 MaritalStatus 字符串的格式

在参数中用这个替换它

MaritalStatus = "Married/\u0935\u093f\u0935\u093e\u0939\u093f\u0924"

【讨论】:

它是由编辑器或编译器创建的默认字符串,我这样输入:Married/विवाहित 如果可能的话,你能不能试着改变这个值,这样你就可以确定问题不是因为这个?【参考方案2】:

我认为您有 Api 功能问题(因为失败错误显示“请求失败:内部服务器错误 (500)”)。

你应该在 AFHTTPSessionManager 失败代码中写下这段代码(下面 ->NSLog(@"request failed >>>> :%@",error);)

NSData *data_error = [error.userInfo objectForKey:@"com.alamofire.serialization.response.error.data"];

xmldata = [[NSMutableString alloc] initWithData:data_error encoding:NSASCIIStringEncoding];

xmldata = [[self stringReplaceForDecoding:xmldata] mutableCopy];

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

NSLog(@"Api error %@",xmldata);

这将在您的 Api 函数中打印实际错误。

【讨论】:

【参考方案3】:

哦,我建议你打印 API 的响应 可能不是 JSON

【讨论】:

您想使用 Format JSON 获得响应,是吗?如果响应不是 JSON 格式,您将收到错误“字符 0 附近的值无效。”,因此,您应该得到响应,而不是 responseJSON 以获得任何格式的结果 首先,您必须修复错误 "Code=-1011 "Request failed: internal server error (500)"

以上是关于使用 AFNetworking 的 API 请求出错的主要内容,如果未能解决你的问题,请参考以下文章

使用 AFNetworking 的 API 请求出错

使用 AFNetworking 获取 OMDb API 数据

使用 AFNetworking 2.0 访问 DailyMile API

如何通过 AFNetworking API 调用使用 gzip 解压?

如何查看 AFNetworking 的 Swift API?

AFNetworking 2.0 和授权 Imgur API