使用AFNetworking请求出错提示:Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.

本文介绍使用AFNetworking请求非JSON格式数据时出现的问题及解决方法。通过调整响应序列化方式,可以避免将HTML等非JSON数据误解析为JSON格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
[manager GET:@" http://www.baidu.com" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSString *result = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@",result);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error);
}];
在AFNetworking中 上述代码将会 输出

 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8ea19a0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

这是因为 AFNetworking默认把响应结果当成json来处理,(默认manager.responseSerializer = [AFJSONResponseSerializer serializer]) ,很显然,我们请求的百度首页 返回的并不是一个json文本,而是一个html网页,但是AFNetworking并不知道,它坚信请求的结果就是一个json文本!然后固执地以json的形式去解析,显然没办法把一个网页解析成一个字典或者数组,所以产生了上述错误.

然而,我们期望它能够正确地处理这个情形,而不是提示一个错误.
这时候 你必须告诉AFNetworking:别把这个网页当json来处理!
只需要在发送请求前加入:manager.responseSerializer = [AFHTTPResponseSerializer serializer]

AFHTTPSessionManager *manager =[AFHTTPSessionManager manager]; 
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 
[manager GET:@"http://www.baidu.com" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) { 
       NSString *result = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]; 
       NSLog(@"%@",result); 
} failure:^(NSURLSessionDataTask *task, NSError *error) { 
      

Linking files for Subswath 1 ... Linking files for Subswath 2 ... Linking files for Subswath 3 ... [1] 22215 [2] 22216 [3] 22217 [3] - Done p2p_processing.csh S1_TOPS ... [2] - Done p2p_processing.csh S1_TOPS ... [1] + Done p2p_processing.csh S1_TOPS ... *** buffer overflow detected ***: terminated Aborted (core dumped) *** buffer overflow detected ***: terminated Aborted (core dumped) *** buffer overflow detected ***: terminated Aborted (core dumped) Merging START Number of Files to be merged is 3 merge_swath [ERROR]: Cannot find file ../F1/intf/2025056_2025104/phasefilt.grd merge_swath [ERROR]: Cannot find file ../F1/intf/2025056_2025104/phasefilt.grd merge_swath [ERROR]: File ../F1/intf/2025056_2025104/phasefilt.grd not found [Session merge_swath (0)]: Error returned from GMT API: GMT_FILE_NOT_FOUND (16) cannot open grids ../F1/intf/2025056_2025104/phasefilt.grd Number of Files to be merged is 3 merge_swath [ERROR]: Cannot find file ../F1/intf/2025056_2025104/corr.grd merge_swath [ERROR]: Cannot find file ../F1/intf/2025056_2025104/corr.grd merge_swath [ERROR]: File ../F1/intf/2025056_2025104/corr.grd not found [Session merge_swath (0)]: Error returned from GMT API: GMT_FILE_NOT_FOUND (16) cannot open grids ../F1/intf/2025056_2025104/corr.grd Number of Files to be merged is 3 merge_swath [ERROR]: Cannot find file ../F1/intf/2025056_2025104/mask.grd merge_swath [ERROR]: Cannot find file ../F1/intf/2025056_2025104/mask.grd merge_swath [ERROR]: File ../F1/intf/2025056_2025104/mask.grd not found [Session merge_swath (0)]: Error returned from GMT API: GMT_FILE_NOT_FOUND (16) cannot open grids ../F1/intf/2025056_2025104/mask.grd Merging END Recomputing the projection LUT... grep: S1_20250226_113936_F1.PRM: 没有那个文件或目录 error - could not open file S1_20250226_113936_F1.PRM couldn't open master.PRM Usage: SAT_llt2rat master.PRM prec [-bo[s|d]] < inputfile > outputfile master.PRM - parameter file for master image and points to LED orbit file precise - (0) standard back geocoding, (1) - polynomial refinenent (slower) inputfile - lon, lat, elevation [ASCII] outputfile - range, azimuth, elevation(ref to radius in PRM), lon, lat [ASCII default] -bos or -bod - binary single or double precision output (only output results within data coverage, PRM num_lines, num_rng_bins ) example: SAT_llt2rat master.PRM 0 < topo.llt > topo.ratll error - could not open file S1_20250226_113936_F1.PRM grdinfo [ERROR]: Cannot find file phasefilt.grd grdinfo [ERROR]: Must specify one or more input files Usage: landmask.csh region_cut[0/10600/0/27648] make a landmask in radar coordinates, needs to run with trans.dat NOTE: The region_cut can be specified in batch.config file SNAPHU.CSH - START threshold_snaphu: 0.1 grdmath [ERROR]: corr_patch.grd is not a number, operator or file name grdmath [ERROR]: corr_patch.grd is not a number, operator or file name grdmath [ERROR]: mask2_patch.grd is not a number, operator or file name grd2xyz [ERROR]: Cannot find file phase_patch.grd grd2xyz [ERROR]: Cannot find file phase_patch.grd grd2xyz [ERROR]: File phase_patch.grd not found [Session gmt (0)]: Error returned from GMT API: GMT_FILE_NOT_FOUND (16) grd2xyz [ERROR]: Cannot find file corr_tmp.grd grd2xyz [ERROR]: Cannot find file corr_tmp.grd grd2xyz [ERROR]: File corr_tmp.grd not found [Session gmt (0)]: Error returned from GMT API: GMT_FILE_NOT_FOUND (16) unwrapping phase with snaphu - higher threshold for faster unwrapping grdinfo [ERROR]: Cannot find file phase_patch.grd grdinfo [ERROR]: Must specify one or more input files snaphu v2.0.4 32 parameters input from file /usr/local/GMTSAR/share/gmtsar/snaphu/config/snaphu.conf.brief (297 lines total) not enough input arguments. type snaphu -h for help grdinfo [ERROR]: Cannot find file phase_patch.grd grdinfo [ERROR]: Must specify one or more input files grdinfo [ERROR]: Cannot find file phase_patch.grd grdinfo [ERROR]: Must specify one or more input files xyz2grd [ERROR]: Must specify -R option xyz2grd [ERROR]: Option -I: Must specify positive increment(s) grdinfo [ERROR]: Cannot find file phase_patch.grd grdinfo [ERROR]: Must specify one or more input files grdinfo [ERROR]: Cannot find file phase_patch.grd grdinfo [ERROR]: Must specify one or more input files xyz2grd [ERROR]: Must specify -R option xyz2grd [ERROR]: Option -I: Must specify positive increment(s) grdmath [ERROR]: tmp.grd is not a number, operator or file name mv: 对 'tmp.grd' 调用 stat 失败: 没有那个文件或目录 grdgradient [ERROR]: Cannot find file unwrap.grd grdinfo [ERROR]: Cannot find file unwrap.grd grdinfo [ERROR]: Must specify one or more input files makecpt [ERROR]: Option T: Must specify valid min[/max[/inc[<unit>|+n]]] option grdimage [ERROR]: Cannot find file unwrap.grd grdimage [ERROR]: Option -I: Must specify intensity file, value, or modifiers psscale [WARNING]: No country code matching unwrap.grd (skipped) psscale [ERROR]: Color palette table unwrap.cpt is empty [Session gmt (0)]: Error returned from GMT API: GMT_CPT_READ_ERROR (8) [Session gmt (0)]: Error returned from GMT API: GMT_CPT_READ_ERROR (8) psconvert [ERROR]: The file unwrap.ps has no BoundingBox in the first 20 lines or last 256 bytes. Use -A option. Unwrapped phase map: unwrap.pdf SNAPHU.CSH - END GEOCODE-START proj_ra2ll.csh grd2xyz [ERROR]: Cannot find file phasefilt.grd grd2xyz [ERROR]: Cannot find file phasefilt.grd grd2xyz [ERROR]: File phasefilt.grd not found [Session gmt (0)]: Error returned from GMT API: GMT_FILE_NOT_FOUND (16) gmtinfo [ERROR]: No input data found! surface [ERROR]: Must specify -R option surface [INFORMATION]: Provides 5, expects 3-column binary data surface [ERROR]: Must specify -R option surface [INFORMATION]: Provides 5, expects 3-column binary data grdtrack [ERROR]: Cannot find file raln.grd grdtrack [ERROR]: Cannot find file ralt.grd grdtrack [ERROR]: Must specify -G at least once gmtconvert [WARNING]: File <stdin> is empty! gmtconvert [WARNING]: No data records provided Sampling in geocoordinates with 50 meter pixels ... gmtinfo [ERROR]: No input data found! range: Subscript out of range. gmtmath [ERROR]: Operation "DIV" requires 2 operands gmtmath [ERROR]: Operation "MUL" requires 2 operands gmtinfo [ERROR]: No input data found! blockmedian [ERROR]: Unable to decode as a floating point number blockmedian [ERROR]: Option -I parsing failure. Correct syntax: -I<xinc>[+e|n][/<yinc>[+e|n]] Specify increment(s) and optionally append units or modifiers. For geographic regions in degrees you can optionally append units from this list: (d)egree [Default], (m)inute, (s)econd, m(e)ter, (f)oot, (k)ilometer, (M)ile, (n)autical mile, s(u)rvey foot. +e Adjust the region to fit increments [Adjust increment to fit domain]. +n Increment specifies the number of nodes instead. Then, the actual increments are calculated from the given domain and node- registration settings (see Appendix B for details). Note: If -R<grdfile> was used then -I (and -R and maybe -r) have been set; use -I to override those increments. blockmedian [ERROR]: Must specify -R option blockmedian [ERROR]: Option -I: Must specify positive increment(s) blockmedian [INFORMATION]: Provides 3, expects 3-column binary data xyz2grd [ERROR]: Unable to decode as a floating point number xyz2grd [ERROR]: Option -I parsing failure. Correct syntax: -I<xinc>[+e|n][/<yinc>[+e|n]] Specify increment(s) and optionally append units or modifiers. For geographic regions in degrees you can optionally append units from this list: (d)egree [Default], (m)inute, (s)econd, m(e)ter, (f)oot, (k)ilometer, (M)ile, (n)autical mile, s(u)rvey foot. +e Adjust the region to fit increments [Adjust increment to fit domain]. +n Increment specifies the number of nodes instead. Then, the actual increments are calculated from the given domain and node- registration settings (see Appendix B for details). Note: If -R<grdfile> was used then -I (and -R and maybe -r) have been set; use -I to override those increments. xyz2grd [ERROR]: Must specify -R option xyz2grd [ERROR]: Option -I: Must specify positive increment(s) grdmath [ERROR]: corr.grd is not a number, operator or file name grdmath [ERROR]: phasefilt.grd is not a number, operator or file name proj_ra2ll.csh grd2xyz [ERROR]: Cannot find file phasefilt_mask.grd grd2xyz [ERROR]: Cannot find file phasefilt_mask.grd grd2xyz [ERROR]: File phasefilt_mask.grd not found [Session gmt (0)]: Error returned from GMT API: GMT_FILE_NOT_FOUND (16) gmtinfo [ERROR]: No input data found! surface [ERROR]: Must specify -R option surface [INFORMATION]: Provides 5, expects 3-column binary data surface [ERROR]: Must specify -R option surface [INFORMATION]: Provides 5, expects 3-column binary data grdtrack [ERROR]: Cannot find file raln.grd grdtrack [ERROR]: Cannot find file ralt.grd grdtrack [ERROR]: Must specify -G at least once gmtconvert [WARNING]: File <stdin> is empty! gmtconvert [WARNING]: No data records provided Sampling in geocoordinates with 50 meter pixels ... gmtinfo [ERROR]: No input data found! range: Subscript out of range. gmtmath [ERROR]: Operation "DIV" requires 2 operands gmtmath [ERROR]: Operation "MUL" requires 2 operands gmtinfo [ERROR]: No input data found! blockmedian [ERROR]: Unable to decode as a floating point number blockmedian [ERROR]: Option -I parsing failure. Correct syntax: -I<xinc>[+e|n][/<yinc>[+e|n]] Specify increment(s) and optionally append units or modifiers. For geographic regions in degrees you can optionally append units from this list: (d)egree [Default], (m)inute, (s)econd, m(e)ter, (f)oot, (k)ilometer, (M)ile, (n)autical mile, s(u)rvey foot. +e Adjust the region to fit increments [Adjust increment to fit domain]. +n Increment specifies the number of nodes instead. Then, the actual increments are calculated from the given domain and node- registration settings (see Appendix B for details). Note: If -R<grdfile> was used then -I (and -R and maybe -r) have been set; use -I to override those increments. blockmedian [ERROR]: Must specify -R option blockmedian [ERROR]: Option -I: Must specify positive increment(s) blockmedian [INFORMATION]: Provides 3, expects 3-column binary data xyz2grd [ERROR]: Unable to decode as a floating point number xyz2grd [ERROR]: Option -I parsing failure. Correct syntax: -I<xinc>[+e|n][/<yinc>[+e|n]] Specify increment(s) and optionally append units or modifiers. For geographic regions in degrees you can optionally append units from this list: (d)egree [Default], (m)inute, (s)econd, m(e)ter, (f)oot, (k)ilometer, (M)ile, (n)autical mile, s(u)rvey foot. +e Adjust the region to fit increments [Adjust increment to fit domain]. +n Increment specifies the number of nodes instead. Then, the actual increments are calculated from the given domain and node- registration settings (see Appendix B for details). Note: If -R<grdfile> was used then -I (and -R and maybe -r) have been set; use -I to override those increments. xyz2grd [ERROR]: Must specify -R option xyz2grd [ERROR]: Option -I: Must specify positive increment(s) proj_ra2ll.csh grd2xyz [ERROR]: Cannot find file corr.grd grd2xyz [ERROR]: Cannot find file corr.grd grd2xyz [ERROR]: File corr.grd not found [Session gmt (0)]: Error returned from GMT API: GMT_FILE_NOT_FOUND (16) gmtinfo [ERROR]: No input data found! surface [ERROR]: Must specify -R option surface [INFORMATION]: Provides 5, expects 3-column binary data surface [ERROR]: Must specify -R option surface [INFORMATION]: Provides 5, expects 3-column binary data grdtrack [ERROR]: Cannot find file raln.grd grdtrack [ERROR]: Cannot find file ralt.grd grdtrack [ERROR]: Must specify -G at least once gmtconvert [WARNING]: File <stdin> is empty! gmtconvert [WARNING]: No data records provided Sampling in geocoordinates with 50 meter pixels ... gmtinfo [ERROR]: No input data found! range: Subscript out of range. gmtmath [ERROR]: Operation "DIV" requires 2 operands gmtmath [ERROR]: Operation "MUL" requires 2 operands gmtinfo [ERROR]: No input data found! blockmedian [ERROR]: Unable to decode as a floating point number blockmedian [ERROR]: Option -I parsing failure. Correct syntax: -I<xinc>[+e|n][/<yinc>[+e|n]] Specify increment(s) and optionally append units or modifiers. For geographic regions in degrees you can optionally append units from this list: (d)egree [Default], (m)inute, (s)econd, m(e)ter, (f)oot, (k)ilometer, (M)ile, (n)autical mile, s(u)rvey foot. +e Adjust the region to fit increments [Adjust increment to fit domain]. +n Increment specifies the number of nodes instead. Then, the actual increments are calculated from the given domain and node- registration settings (see Appendix B for details). Note: If -R<grdfile> was used then -I (and -R and maybe -r) have been set; use -I to override those increments. blockmedian [ERROR]: Must specify -R option blockmedian [ERROR]: Option -I: Must specify positive increment(s) blockmedian [INFORMATION]: Provides 3, expects 3-column binary data xyz2grd [ERROR]: Unable to decode as a floating point number xyz2grd [ERROR]: Option -I parsing failure. Correct syntax: -I<xinc>[+e|n][/<yinc>[+e|n]] Specify increment(s) and optionally append units or modifiers. For geographic regions in degrees you can optionally append units from this list: (d)egree [Default], (m)inute, (s)econd, m(e)ter, (f)oot, (k)ilometer, (M)ile, (n)autical mile, s(u)rvey foot. +e Adjust the region to fit increments [Adjust increment to fit domain]. +n Increment specifies the number of nodes instead. Then, the actual increments are calculated from the given domain and node- registration settings (see Appendix B for details). Note: If -R<grdfile> was used then -I (and -R and maybe -r) have been set; use -I to override those increments. xyz2grd [ERROR]: Must specify -R option xyz2grd [ERROR]: Option -I: Must specify positive increment(s) grdinfo [ERROR]: Cannot find file corr.grd grdinfo [ERROR]: Must specify one or more input files makecpt [ERROR]: Option T: Must specify valid min/max/inc[<unit>|+n] option grdinfo [ERROR]: Cannot find file phasefilt_ll.grd grdinfo [ERROR]: Must specify one or more input files gmtmath [ERROR]: Operation "INV" requires 1 operands grdimage [ERROR]: Cannot find file phasefilt_ll.grd Make phasefilt_ll.kml and phasefilt_ll.png psconvert [ERROR]: Option -E: No argument provided rm: No match. grdinfo [ERROR]: Cannot find file phasefilt_mask_ll.grd grdinfo [ERROR]: Must specify one or more input files gmtmath [ERROR]: Operation "INV" requires 1 operands grdimage [ERROR]: Cannot find file phasefilt_mask_ll.grd Make phasefilt_mask_ll.kml and phasefilt_mask_ll.png psconvert [ERROR]: Option -E: No argument provided rm: No match. grdinfo [ERROR]: Cannot find file corr_ll.grd grdinfo [ERROR]: Must specify one or more input files gmtmath [ERROR]: Operation "INV" requires 1 operands grdimage [ERROR]: Cannot find file corr_ll.grd Make corr_ll.kml and corr_ll.png psconvert [ERROR]: Option -E: No argument provided rm: No match. GEOCODE END rm: No match. DInSAR processing completed successfully.
最新发布
05-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值