zoukankan      html  css  js  c++  java
  • UR中文请求

    iOS开发-url包含中文报错解决办法

    一只BT浮出来  

    经常, 我们用通过这样的方法调用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];
    

      

  • 相关阅读:
    JAVA学习---文件和流
    JAVA学习---集合和工具类
    JAVA学习---异常
    python-time模块
    python-并发编程
    python-网络编程
    python-并发编程之进程
    python-面向对象之多态
    python-面向对象之反射
    python-面向对象之封装
  • 原文地址:https://www.cnblogs.com/hl666/p/3790133.html
Copyright © 2011-2022 走看看