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];



查看全文
  • 相关阅读:
    windows环境下生成ssh keys
    手写简易Ajax-结合Promise
    Microsoft 登陆微软账号一直加载不进去 解决方案整理
    win10如何开启卓越性能
    win10开启上帝模式
    解决网页禁止复制文字
    module 'sklearn' has no attribute 'svm'
    sklearn的SVM的decision_function_shape的ovo和ovr
    Can not squeeze dim[1], expected a dimension of 1
    python/numpy随机选取训练集/测试集索引
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10739905.html
  • Copyright © 2011-2022 走看看