http://blog.csdn.net/zengraoli/article/details/12918369 在blog文 iOS使用ASIHttpRequestJson与服务器段脚本进行登陆验证 中,没有仔细的说清楚,到底是如何交互的,很是抱歉;毕竟我用php也仅限于这几天。。 先来看,我刚才写的一个php端: [php] view p
http://blog.csdn.net/zengraoli/article/details/12918369
在blog文
iOS使用ASIHttpRequest+Json与服务器段脚本进行登陆验证
中,没有仔细的说清楚,到底是如何交互的,很是抱歉;毕竟我用php也仅限于这几天。。
先来看,我刚才写的一个php端:
[php]view plaincopyprint?
-
- $arr;
- functiontraverse($path='.')
- {
- $current_dir= opendir($path);//opendir()返回一个目录句柄,失败返回false
- $directory_arr;
- $file_arr;
- $directory_index= 1;
- $file_index= 1;
- Global$arr;
- $arr_index= 0;
- while(($file= readdir($current_dir)) !== false)
- {//readdir()返回打开目录句柄中的一个条目
- $sub_dir=$path. DIRECTORY_SEPARATOR .$file;//构建子目录路径
- if($file=='.'||$file=='..')
- {
- continue;
- }
- elseif(is_dir($sub_dir))
- {//如果是目录,进行递归
- // echo 'Directory ' . $file . ':
';
- $string="Directory";
- $string.=$directory_index;
- $directory_arr[$string] =$file;
- $directory_index++;
- traverse($sub_dir);
- // print_r($directory_arr);
- }
- else
- {//如果是文件,直接输出
- // echo 'File in Directory ' . $path . ': ' . $file . '
';
- $file_arr[$file_index] =$path.'\\' . $file . '
';
- $file_index++;
- }
- };
- $arr["dir_count"] =count($directory_arr);
- // print_r($file_arr);
- // print_r(count($file_arr));
- // echo '
';
- // echo "==============================";
- // echo '
';
- // 有一个是title需要先减出来,还有一半是.txt
- $arr[$path] = (count($file_arr) - 1) / 2;
- }
- traverse('Images');
- // print_r($arr);
- // print_r(json_encode($arr));
- $resultJson= json_encode($arr);
- echo$resultJson;
- ?>
在服务器端直接运行这个php脚本之后得到的页面如下:
![iOS通过ASIHttpRequest接收php端发送的Json数据](//m.sbmmt.com/inc/test.jsp?url=http%3A%2F%2Fimg.blog.csdn.net%2F20131021214713812%3Fwatermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvemVuZ3Jhb2xp%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FCenter&refer=http%3A%2F%2Fblog.csdn.net%2Fyuanya%2Farticle%2Fdetails%2F18740665)
这是一个获取当前webroot目录下,Images文件夹里面的目录个数,和这些目录个数下面.jpg文件个数的一个demo
这是其中一个day1的内容:
![iOS通过ASIHttpRequest接收php端发送的Json数据](//m.sbmmt.com/inc/test.jsp?url=http%3A%2F%2Fimg.blog.csdn.net%2F20131021212815593%3Fwatermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvemVuZ3Jhb2xp%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FCenter&refer=http%3A%2F%2Fblog.csdn.net%2Fyuanya%2Farticle%2Fdetails%2F18740665)
在对应的iOS端,这样写:
[cpp]view plaincopyprint?
- //
- // ViewController.m
- // Demo
- //
- // Created by zengraoli on 13-10-20.
- // Copyright (c) 2013年 zeng. All rights reserved.
- //
- #import "ViewController.h"
- #import "UIView+Additon.h"
- @interface ViewController ()
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- [self getResourcesCount];
- }
- -(void)getResourcesCount
- {
- NSString *baseurl=@"get_resources_count.php";
- NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",host_url,baseurl]];
- [self setRequest:[ASIHTTPRequest requestWithURL:url]];
- [_request addRequestHeader:@"User-Agent"value:@"ASIHTTPRequest"];
- [_request startSynchronous];
- //显示网络请求信息在status bar上
- [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:YES];
- if(_request)
- {
- if([_request error])
- {
- NSLog(@"error");
- }
- elseif([_request responseString])
- {
- NSString *result = [_request responseString];
- // NSLog(@"%@",result);
- NSDictionary *mydict = [result JSONValue];
- describeDictionary(mydict);
- }
- }
- else
- {
- NSLog(@"request is nil.");
- }
- }
- voiddescribeDictionary(NSDictionary *dict)
- {
- NSArray *keys;
- inti, count;
- id key, value;
- keys = [dict allKeys];
- count = [keys count];
- for(i = 0; i
- {
- key = [keys objectAtIndex: i];
- value = [dict objectForKey: key];
- NSLog (@"Key: %@ for value: %@", key, value);
- }
- }
- @end
这是调用这段代码后,解析Json数据得到的结果:
![iOS通过ASIHttpRequest接收php端发送的Json数据](//m.sbmmt.com/inc/test.jsp?url=http%3A%2F%2Fimg.blog.csdn.net%2F20131021215219296%3Fwatermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvemVuZ3Jhb2xp%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FCenter&refer=http%3A%2F%2Fblog.csdn.net%2Fyuanya%2Farticle%2Fdetails%2F18740665)