你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS POST請求 有中文的時候

iOS POST請求 有中文的時候

編輯:IOS開發綜合

镔哥,最近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 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:selfcancelButtonTitle:@"確定" otherButtonTitles:nil, nil];

[alert show];

}

}





  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved