码迷,mamicode.com
首页 > 移动开发 > 详细

iOS POST请求 有中文的时候

时间:2014-12-03 19:17:49      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:ios post请求 中文的时候   数据请求   

镔哥,最近post请求遇到一些问题,当有中文的时候是请求不成功

下面镔哥写两种案例:

1:通常post请求:(有中文不成功)

//1:设置URL

    NSString *host = HOST;

    NSString *usename = @"fuck";

    NSString *queryString = [NSString stringWithFormat:@"/app/clickTableScreen?userName=%@&idfa=%@&operator=%@&systemVersion=%@&networkState=%@&deviceName=%@&memorySize=%@&model=%@",usename,adId, [self checkCarrier],phoneVersion,netStr,userPhoneName,totalDiskSpaceStr, [LoginViewController deviceString]] ;

     NSLog(@"querString:%@",queryString);

   //完整的设置参数

    NSString *urlString = [NSString stringWithFormat:@"%@%@",host,queryString];

    NSLog(@"参数:%@",urlString);

    //得到完整的url

    NSURL *url1 = [NSURL URLWithString:urlString];



    //2:Request请求

            NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];

    [request setHTTPMethod:@"POST"];

    //第三步,连接服务器

    NSError * error = nil;

    NSData *reqData  = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

    

//    //3:将请求转换二进制

//    NSData *reqData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    if(reqData == nil){

        if(error){//这样写有个好处,就是如果解析数据不成功,系统会列出原因,我就因为写了这句话才知道出现什么原因:Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x14ed24c0 {NSUnderlyingError=0x

             NSLog(@"error = %@", error);

        }

        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不稳定,请稍后尝试!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"ok", nil];

        [alert show];

        return;

    }else{

        NSLog(@"error = %@", error);

    }

    NSDictionary *reqDic=[NSJSONSerialization JSONObjectWithData:reqData options:NSJSONReadingAllowFragments error:nil];

    NSString *info = [NSString stringWithFormat:@"%@",[reqDic objectForKey:@"info"]];

    NSLog(@"参数字典:%@",reqDic);

    //4:打印数据

    NSString *errcode = [NSString stringWithFormat:@"%@", [reqDic objectForKey:@"errcode"]];

    if ([errcode isEqualToString:@"0"]) {

        NSLog(@"成功了");

    }else

    {

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"获取广告标识符失败" message:info delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

        [alert show];

    }

}



2:第二种写法对比(其实就是加了一句话)

/1:设置URL

    NSString *host = HOST;

    NSString *usename = @"fuck";

    NSString *queryString = [NSString stringWithFormat:@"/app/clickTableScreen?userName=%@&idfa=%@&operator=%@&systemVersion=%@&networkState=%@&deviceName=%@&memorySize=%@&model=%@",usename,adId, [self checkCarrier],phoneVersion,netStr,userPhoneName,totalDiskSpaceStr, [LoginViewController deviceString]] ;

     NSLog(@"querString:%@",queryString);

   //完整的设置参数

    NSString *urlString = [NSString stringWithFormat:@"%@%@",host,queryString];

    NSLog(@"参数:%@",urlString);

    //得到完整的url

    //有中文的时候要转码

    NSString * urlstr = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

    NSURL * url = [NSURL URLWithString:urlstr];



    //2:Request请求

            NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];

    [request setHTTPMethod:@"POST"];

    //第三步,连接服务器

    NSError * error = nil;

    NSData *reqData  = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

    

//    //3:将请求转换二进制

//    NSData *reqData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    if(reqData == nil){

        if(error){//这样写有个好处,就是如果解析数据不成功,系统会列出原因,我就因为写了这句话才知道出现什么原因:Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x14ed24c0 {NSUnderlyingError=0x

             NSLog(@"error = %@", error);

        }

        UIAlertView * alert = [[UIAlertView allocinitWithTitle:@"提示" message:@"网络不稳定,请稍后尝试!"delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"ok"nil];

        [alert show];

        return;

    }else{

        NSLog(@"error = %@", error);

    }

    NSDictionary *reqDic=[NSJSONSerialization JSONObjectWithData:reqData options:NSJSONReadingAllowFragments error:nil];

    NSString *info = [NSString stringWithFormat:@"%@",[reqDic objectForKey:@"info"]];

    NSLog(@"参数字典:%@",reqDic);

    //4:打印数据

    NSString *errcode = [NSString stringWithFormat:@"%@", [reqDic objectForKey:@"errcode"]];

    if ([errcode isEqualToString:@"0"]) {

        NSLog(@"成功了");

    }else

    {

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"获取广告标识符失败" message:info delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nilnil];

        [alert show];

    }

}





iOS POST请求 有中文的时候

标签:ios post请求 中文的时候   数据请求   

原文地址:http://blog.csdn.net/sammyieveo/article/details/41700119

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!