zoukankan      html  css  js  c++  java
  • iOS开发-url包括中文报错解决的方法

    常常, 我们用通过这个方案调用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中Collections.sort() 排序函数的用法
    [SDOI2017]数字表格
    [SDOI2008]洞穴勘测
    How to copy a file from xp to linux?
    A java class
    Linux Mysql Charset utf8
    Linux environment variables
    get vars from url or request
    你在视网膜技术上太逊了,快来补充知识吧! java程序员
    Activity消息传递与消息回传模板 java程序员
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10739905.html
  • Copyright © 2011-2022 走看看