鑌哥,最近post要求遇到1些問(wèn)題,當(dāng)有中文的時(shí)候是要求不成功
下面鑌哥寫兩種案例:
1:通常post要求:(有中文不成功)
//1:設(shè)置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);
//完全的設(shè)置參數(shù)
NSString *urlString = [NSString stringWithFormat:@"%@%@",host,queryString];
NSLog(@"參數(shù):%@",urlString);
//得到完全的url
NSURL *url1 = [NSURL URLWithString:urlString];
//2:Request要求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[request setHTTPMethod:@"POST"];
//第3步,連接服務(wù)器
NSError * error = nil;
NSData *reqData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
// //3:將要求轉(zhuǎn)換2進(jìn)制
// NSData *reqData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if(reqData == nil){
if(error){//這樣寫有個(gè)好處,就是如果解析數(shù)據(jù)不成功,系統(tǒng)會(huì)列出緣由,我就由于寫了這句話才知道出現(xiàn)甚么緣由:(Error Domain=NSURLErrorDomain Code=⑴002 "unsupported URL" UserInfo=0x14ed24c0 {NSUnderlyingError=0x)
NSLog(@"error = %@", error);
}
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"網(wǎng)絡(luò)不穩(wěn)定,請(qǐng)稍后嘗試!" 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(@"參數(shù)字典:%@",reqDic);
//4:打印數(shù)據(jù)
NSString *errcode = [NSString stringWithFormat:@"%@", [reqDic objectForKey:@"errcode"]];
if ([errcode isEqualToString:@"0"]) {
NSLog(@"成功了");
}else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"獲得廣告標(biāo)識(shí)符失敗" message:info delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];
[alert show];
}
}
/1:設(shè)置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);
//完全的設(shè)置參數(shù)
NSString *urlString = [NSString stringWithFormat:@"%@%@",host,queryString];
NSLog(@"參數(shù):%@",urlString);
//得到完全的url
//有中文的時(shí)候要轉(zhuǎn)碼
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"];
//第3步,連接服務(wù)器
NSError * error = nil;
NSData *reqData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
// //3:將要求轉(zhuǎn)換2進(jìn)制
// NSData *reqData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if(reqData == nil){
if(error){//這樣寫有個(gè)好處,就是如果解析數(shù)據(jù)不成功,系統(tǒng)會(huì)列出緣由,我就由于寫了這句話才知道出現(xiàn)甚么緣由:(Error Domain=NSURLErrorDomain Code=⑴002 "unsupported URL" UserInfo=0x14ed24c0 {NSUnderlyingError=0x)
NSLog(@"error = %@", error);
}
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"網(wǎng)絡(luò)不穩(wěn)定,請(qǐng)稍后嘗試!"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(@"參數(shù)字典:%@",reqDic);
//4:打印數(shù)據(jù)
NSString *errcode = [NSString stringWithFormat:@"%@", [reqDic objectForKey:@"errcode"]];
if ([errcode isEqualToString:@"0"]) {
NSLog(@"成功了");
}else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"獲得廣告標(biāo)識(shí)符失敗" message:info delegate:selfcancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];
[alert show];
}
}