首页 > 移动平台 > 详细

iOS开发-url包括中文报错解决的方法

时间:2019-04-20 11:52:45      阅读:255      评论:0      收藏:0      [点我收藏+]

常常, 我们用通过这个方案调用API。

    NSString* urlString = [NSString stringWithFormat:@"http://api.douban.com/v2/movie/search?q=%@", content];
    
    NSURL *url = [NSURL URLWithString:urlString];
    testRequest = [ASIHTTPRequest requestWithURL:url];
    [testRequest setDelegate:self];
    [testRequest startAsynchronous];



可是, 当路径中存在中文的时候. ASI就无法正常訪问那个url。

报错例如以下:

Error Domain=ASIHTTPRequestErrorDomain Code=5 "Unable to create request (bad url?)" UserInfo=0x109697090 {NSLocalizedDescription=Unable to create request (bad url?

)}



解决的方法:

转码.

	urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


完整代码:

    NSString* urlString = [NSString stringWithFormat:@"http://api.douban.com/v2/movie/search?

q=%@", content]; urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:urlString]; testRequest = [ASIHTTPRequest requestWithURL:url]; [testRequest setDelegate:self]; [testRequest startAsynchronous];



iOS开发-url包括中文报错解决的方法

原文:https://www.cnblogs.com/ldxsuanfa/p/10739905.html

(0)
(0)
   
举报
评论 一句话评论(0
分享档案
最新文章
教程昨日排行
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!