网络数据请求方式(SOAP和JSON)
2013-06-04 14:47
316 查看
从网络上进行数据的请求主要有SOAP方式和JSON方式,下面是我在看天气预报的例子时比较的两种方式的不同,在这里Mark一下。
SOAP方式:
使用SOAP获得天气预报支持的省直辖市信息
//封装soap请求消息
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"
"<getSupportProvince xmlns=\"http://WebXml.com.cn/\">\n"
"</getSupportProvince>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",@"5"
];
//请求发送到的路径
NSURL *url = [NSURL
URLWithString:@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest
requestWithURL:url];
NSString *msgLength = [NSString
stringWithFormat:@"%d", [soapMessage
length]];
//以下对请求信息添加属性前四句是必有的,第五句是soap信息。
[theRequest
addValue: @"text/xml; charset=utf-8"
forHTTPHeaderField:@"Content-Type"];
[theRequest
addValue: @"http://WebXml.com.cn/getSupportProvince"
forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength
forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest
setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
//请求
NSURLConnection *theConnection = [[NSURLConnection
alloc] initWithRequest:theRequest
delegate:self];
//如果连接已经建好,则初始化data
if( theConnection )
{
webData = [[NSMutableData
data] retain];
}
JSON方式:
使用iOS自带解析类NSJSONSerialization方法解析(ios5以上版本支持)
//取得苏州的天气情况
NSError *error;
//加载一个NSURL对象
NSURLRequest *request = [NSURLRequest
requestWithURL:[NSURL
URLWithString:@"http://m.weather.com.cn/data/101190401.html"]];
a137
//将请求的数据放置到NSData中
NSData *reponse = [NSURLConnection
sendSynchronousRequest:request returningResponse:nil
error:nil];
//利用ios5自带的解析类NSJSONSerialization从response中解析出的数据放到字典中
NSDictionary *weatherDic = [NSJSONSerialization
JSONObjectWithData:reponse options:NSJSONReadingMutableLeaves
error:&error];
NSDictionary *weather = [weatherDic
objectForKey:@"weatherinfo"];
weatherStr = [NSString
stringWithFormat:@"今天是%@,%@,%@\n天气状况是,%@,%@",[weather objectForKey:@"date_y"],[weather objectForKey:@"week"],[weather objectForKey:@"city"],[weather
objectForKey:@"weather1"],[weather objectForKey:@"temp1"]];
NSLog(@"weather字典里的内容是:%@",weatherDic);
SOAP方式:
使用SOAP获得天气预报支持的省直辖市信息
//封装soap请求消息
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"
"<getSupportProvince xmlns=\"http://WebXml.com.cn/\">\n"
"</getSupportProvince>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",@"5"
];
//请求发送到的路径
NSURL *url = [NSURL
URLWithString:@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest
requestWithURL:url];
NSString *msgLength = [NSString
stringWithFormat:@"%d", [soapMessage
length]];
//以下对请求信息添加属性前四句是必有的,第五句是soap信息。
[theRequest
addValue: @"text/xml; charset=utf-8"
forHTTPHeaderField:@"Content-Type"];
[theRequest
addValue: @"http://WebXml.com.cn/getSupportProvince"
forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength
forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest
setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
//请求
NSURLConnection *theConnection = [[NSURLConnection
alloc] initWithRequest:theRequest
delegate:self];
//如果连接已经建好,则初始化data
if( theConnection )
{
webData = [[NSMutableData
data] retain];
}
JSON方式:
使用iOS自带解析类NSJSONSerialization方法解析(ios5以上版本支持)
//取得苏州的天气情况
NSError *error;
//加载一个NSURL对象
NSURLRequest *request = [NSURLRequest
requestWithURL:[NSURL
URLWithString:@"http://m.weather.com.cn/data/101190401.html"]];
a137
//将请求的数据放置到NSData中
NSData *reponse = [NSURLConnection
sendSynchronousRequest:request returningResponse:nil
error:nil];
//利用ios5自带的解析类NSJSONSerialization从response中解析出的数据放到字典中
NSDictionary *weatherDic = [NSJSONSerialization
JSONObjectWithData:reponse options:NSJSONReadingMutableLeaves
error:&error];
NSDictionary *weather = [weatherDic
objectForKey:@"weatherinfo"];
weatherStr = [NSString
stringWithFormat:@"今天是%@,%@,%@\n天气状况是,%@,%@",[weather objectForKey:@"date_y"],[weather objectForKey:@"week"],[weather objectForKey:@"city"],[weather
objectForKey:@"weather1"],[weather objectForKey:@"temp1"]];
NSLog(@"weather字典里的内容是:%@",weatherDic);
相关文章推荐
- JSON数据与后台跨域交互时候网络请求的发送方式
- RxVolley进行网络请求(get方式),获取json数据
- Android网络编程之Http请求服务器数据(GET方式)
- Post方式请求网络数据
- 网络请求数据 get请求方式 post请求 协议异步连接服务器 block异步连接服务器
- ajax请求当发送post方式application/json格式数据,url后面又带有参数的时候
- 请求网络数据JSON
- Android 手机卫士--构建服务端json、请求网络数据
- Android网络编程之Http请求服务器数据(POST方式)
- Post方式请求网络数据
- Android网络请求与JSON解析和数据抓取小实例
- Android--网络请求与json数据解析的使用
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- Android请求网络数据,json解析-FastJson遇到的问题 首字母大写问题
- json xml 网络请求是用的传输数据的格式
- 使用swiftyJson和Alamofire做网络请求,以及json数据的解析
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- 使用HttpURLConnection和使用HttpClient方式请求网络采用get方式和post方式请求数据
- C++通过HTTP请求Get或Post方式请求Json数据