Home> php教程> php手册> body text

iOS通过ASIHttpRequest接收php端发送的Json数据

WBOY
Release: 2016-06-06 19:58:51
Original
1193 people have browsed it

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?

  1. $arr;
  2. functiontraverse($path='.')
  3. {
  4. $current_dir= opendir($path);//opendir()返回一个目录句柄,失败返回false
  5. $directory_arr;
  6. $file_arr;
  7. $directory_index= 1;
  8. $file_index= 1;
  9. Global$arr;
  10. $arr_index= 0;
  11. while(($file= readdir($current_dir)) !== false)
  12. {//readdir()返回打开目录句柄中的一个条目
  13. $sub_dir=$path. DIRECTORY_SEPARATOR .$file;//构建子目录路径
  14. if($file=='.'||$file=='..')
  15. {
  16. continue;
  17. }
  18. elseif(is_dir($sub_dir))
  19. {//如果是目录,进行递归
  20. // echo 'Directory ' . $file . ':
    ';
  21. $string="Directory";
  22. $string.=$directory_index;
  23. $directory_arr[$string] =$file;
  24. $directory_index++;
  25. traverse($sub_dir);
  26. // print_r($directory_arr);
  27. }
  28. else
  29. {//如果是文件,直接输出
  30. // echo 'File in Directory ' . $path . ': ' . $file . '
    ';
  31. $file_arr[$file_index] =$path.'\\' . $file . '
    ';
  32. $file_index++;
  33. }
  34. };
  35. $arr["dir_count"] =count($directory_arr);
  36. // print_r($file_arr);
  37. // print_r(count($file_arr));
  38. // echo '
    ';
  39. // echo "==============================";
  40. // echo '
    ';
  41. // 有一个是title需要先减出来,还有一半是.txt
  42. $arr[$path] = (count($file_arr) - 1) / 2;
  43. }
  44. traverse('Images');
  45. // print_r($arr);
  46. // print_r(json_encode($arr));
  47. $resultJson= json_encode($arr);
  48. echo$resultJson;
  49. ?>


在服务器端直接运行这个php脚本之后得到的页面如下:

iOS通过ASIHttpRequest接收php端发送的Json数据


这是一个获取当前webroot目录下,Images文件夹里面的目录个数,和这些目录个数下面.jpg文件个数的一个demo

这是其中一个day1的内容:

iOS通过ASIHttpRequest接收php端发送的Json数据


在对应的iOS端,这样写:

[cpp]view plaincopyprint?

  1. //
  2. // ViewController.m
  3. // Demo
  4. //
  5. // Created by zengraoli on 13-10-20.
  6. // Copyright (c) 2013年 zeng. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. #import "UIView+Additon.h"
  10. @interface ViewController ()
  11. @end
  12. @implementation ViewController
  13. - (void)viewDidLoad
  14. {
  15. [super viewDidLoad];
  16. // Do any additional setup after loading the view, typically from a nib.
  17. [self getResourcesCount];
  18. }
  19. -(void)getResourcesCount
  20. {
  21. NSString *baseurl=@"get_resources_count.php";
  22. NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",host_url,baseurl]];
  23. [self setRequest:[ASIHTTPRequest requestWithURL:url]];
  24. [_request addRequestHeader:@"User-Agent"value:@"ASIHTTPRequest"];
  25. [_request startSynchronous];
  26. //显示网络请求信息在status bar上
  27. [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:YES];
  28. if(_request)
  29. {
  30. if([_request error])
  31. {
  32. NSLog(@"error");
  33. }
  34. elseif([_request responseString])
  35. {
  36. NSString *result = [_request responseString];
  37. // NSLog(@"%@",result);
  38. NSDictionary *mydict = [result JSONValue];
  39. describeDictionary(mydict);
  40. }
  41. }
  42. else
  43. {
  44. NSLog(@"request is nil.");
  45. }
  46. }
  47. voiddescribeDictionary(NSDictionary *dict)
  48. {
  49. NSArray *keys;
  50. inti, count;
  51. id key, value;
  52. keys = [dict allKeys];
  53. count = [keys count];
  54. for(i = 0; i
  55. {
  56. key = [keys objectAtIndex: i];
  57. value = [dict objectForKey: key];
  58. NSLog (@"Key: %@ for value: %@", key, value);
  59. }
  60. }
  61. @end


这是调用这段代码后,解析Json数据得到的结果:

iOS通过ASIHttpRequest接收php端发送的Json数据

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!