iOS與Web Service(Php)資料交換
来源:互联网 发布:软件测试培训学校 编辑:程序博客网 时间:2024/05/06 02:41
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1
#define kWSLookingFunURL [NSURL URLWithString:@"http://localhost/wsCatalog.php"]
從web Service讀取資料//讀取web service
-(void)wsLookingFun
{
dispatch_async(kBgQueue, ^{
NSData *data = [NSData dataWithContentsOfURL:kWSLookingFunURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
//讀取後的資料處理
-(void)fetchedData:(NSData*)responseData
{
NSError *error = nil;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSArray *catalogs_ = [json objectForKey:@"catalogs"];
//NSString *jsonFormat = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
//NSLog(@"HTML = %@", jsonFormat);
catalogData_ = [NSArray arrayWithArray:catalogs_];
[self.tableView reloadData];
}
顯示在UITableView上//UITableView資料顯示-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
//catalogData_為陣列,利用indexPath.row將元素取出,放到Dictionary
/*
資料格式
catalog = {
description = "";
id = "\U7f8e\U5f0f\U9910\U5ef3";
timestamp = "2012-03-13 00:59:37";
};
*/
NSDictionary *catalog_ = [catalogData_ objectAtIndex:indexPath.row];
NSLog(@"%@",catalog_);
cell.textLabel.text = [[(NSDictionary*)catalog_ objectForKey:@"catalog"]objectForKey:@"id"];
cell.detailTextLabel.text = [[(NSDictionary*)catalog_ objectForKey:@"catalog"]objectForKey:@"timestamp"];
return cell;
}
資料寫入:先將資料轉為Json格式後,再POST到Web service//資料新增(產生Json的格式)- (IBAction)insertCatalog:(id)sender
{
NSError *error = NULL;
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:
[txtCatalog_ text],@"id",
@"from iphone",@"description"
, nil];
//壓縮成NSData,這時的編碼會成UTF8格式
NSData *jsonData = [NSJSONSerialization
dataWithJSONObject:info
options:NSJSONWritingPrettyPrinted
error:&error];
//完成Json格式後,進行新增
[self insertCatalogWithJson:jsonData];
}
//傳送至web Service
-(void)insertCatalogWithJson:(NSData*)json
{
NSString *urlAsString = [[NSString alloc]initWithString: [kWSLookingFunURL absoluteString]];
urlAsString = [urlAsString stringByAppendingString:@"?action=insert"];
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"POST"];
//直接把NSData(這時的編碼為UTF8)做為傳送的內容
[urlRequest setHTTPBody:json];
NSLog(@"REQUEST: %@", [urlRequest HTTPBody]);
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ([data length] >0 && error == nil)
{
NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %@", html);
//重新從WebService讀取資料
[self wsLookingFun];
}else if ([data length] == 0 && error == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (error != nil)
{
NSLog(@"Error happened = %@", error);
}}];
}
- iOS與Web Service(Php)資料交換
- iOS與Web Service(Php)資料交換
- PHP & Web Service
- php调用web service
- PHP标准Web Service
- ios + web service
- ios调用web service
- PHP,Web,Service简单入门
- PHP中Web Service应用
- 【转】PHP & Web Service[初级]
- PHP Web Service权限验证
- 使用PHP调用Web service
- 使用 php编写web service
- LINUX php 开发Web Service
- PHP中Web Service应用
- Web Service的IOS应用
- IOS下使用WEB SERVICE
- iOS开发中的Web Service
- python 多线程实现洗牌算法(一)
- sql count某个group by出来的分组后,能按条件取出该分组的某个值
- jquery的环境搭建以及初次使用
- 用 Shell 脚本访问 MySQL 数据库
- Android实现左右滑动效果
- iOS與Web Service(Php)資料交換
- spring aop拦截action配置
- Mcrypt响应慢的一个原因
- vector运用
- Delphi获取系统特殊路径
- ora-02429排查
- 第一范式,第二范式,第三范式
- static成员变量,staitic成员函数以及 实现只能实例化3次的类
- JAVA项目和JAVAWEB项目的目录结构比较