您的位置:首页 > 理论基础 > 计算机网络

IOS学习笔记,网络请求,json解析

2014-03-10 15:57 555 查看
创建一个同步的请求,需做如下步骤

1、创建一个NSString 类型的url字符 并把该url转化为NSURL类型

    //设置请求的url
NSString *urlString= @"www.baidu.com";
    NSURL* url = [NSURL URLWithString:urlString];
2、把URL对象赋值到NSURLRequest对象中,如为多个请求,需放入NSMutableURLRequest中。
3、创建一个NSURLConnection的实例,然后
4000
把自己定义好的对象赋值过去

     NSMutableURLRequest*  request= [NSMutableURLRequest new];     //url实例

    [request setURL:url]; 
//设置连接的url
    [request setTimeoutInterval:5];
//设置超时时间。如请求不能够正常获取,等待的时间。

//下面都是请求的一些json格式,具体按照服务器要求的json格式来操作
    [request addValue:[url host] forHTTPHeaderField:@"HOST"];
    [request addValue:@"text/xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"];
    [request addValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request addValue:soapUrl forHTTPHeaderField:@"SOAPAction"];
//请求的方式为同步请求
    [request  setHTTPMethod:@"POST"];

同步请求的获取方法

+ (NSDictionary*)postAndGetData:(NSString*)urlString :(NSMutableDictionary*)dic :(NSString*)soapUrl
{
    
    
    //获取传送过来的json数据
    NSString*JSONString = [dic
JSONRepresentation];
    NSLog(@"JSONString===%@",JSONString);

    
//请求服务器所需的json格式
    NSString *soapMessage = [NSString
stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<service xmlns=\"http://www.thinkhome.com.cn/\">\n"
"<json>%@</json>\n"
"</service>\n"
"</soap:Body>\n"
"</soap:Envelope>\n", JSONString];
    
    //设置请求的url
    NSURL* url = [NSURL
URLWithString:urlString];
//设置请求数据
    NSData*postData = [JSONString
dataUsingEncoding:NSUTF8StringEncoding];
    NSString*postLength = [NSString
stringWithFormat:@"%d", [postData
length]];
    //创建请求
    NSMutableURLRequest*  request= [NSMutableURLRequest
new];
    [request setURL:url];
    [request addValue:[url
host] forHTTPHeaderField:@"HOST"];
    [request addValue:@"text/xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"];
    [request addValue:postLength
forHTTPHeaderField:@"Content-Length"];
    [request addValue:soapUrl
forHTTPHeaderField:@"SOAPAction"];
    [request setTimeoutInterval:5];
    [request  setHTTPMethod:@"POST"];
    [request setHTTPBody:[soapMessage
dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLResponse *response;
    NSData* data=  [NSURLConnection
sendSynchronousRequest:request returningResponse:&response
error:nil];
    NSString* strRet= [[NSString
alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",strRet);
        
 
    
    NSMutableDictionary *dict=[[NSMutableDictionary
alloc]init];
    if([strRet isEqualToString:@""])
    {
        NSMutableDictionary *dict1=[[NSMutableDictionary
alloc]init];
        [dict1 setValue:@"199"
forKey:@"status"];
        
        [dict setValue:dict1
forKey:@"head"];
    }
    else
    {
        NSRange range1=[strRet
rangeOfString:@"{"];
        NSString *str1=[strRet
substringFromIndex:range1.location];
        NSRange range2=[str1
rangeOfString:@"serviceResult"];
        NSString *str2=[str1
substringToIndex:range2.location];
        str2=[str2 substringToIndex:str2.length-2];
        dict=[str2 JSONValue];
    }
    
    return dict;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios json 服务器 数据 url