Heim > php教程 > php手册 > Hauptteil

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

WBOY
Freigeben: 2016-06-06 19:58:51
Original
1228 Leute haben es durchsucht

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


在服务器端直接运行这个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.   
  9. #import "ViewController.h"  
  10. #import "UIView+Additon.h"  
  11.   
  12.   
  13. @interface ViewController ()  
  14.   
  15. @end  
  16.   
  17. @implementation ViewController  
  18.   
  19. - (void)viewDidLoad  
  20. {  
  21.     [super viewDidLoad];  
  22.     // Do any additional setup after loading the view, typically from a nib.  
  23.       
  24.     [self getResourcesCount];  
  25. }  
  26.   
  27. -(void)getResourcesCount  
  28. {  
  29.     NSString *baseurl=@"get_resources_count.php";  
  30.       
  31.     NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",host_url,baseurl]];  
  32.     [self setRequest:[ASIHTTPRequest requestWithURL:url]];  
  33.     [_request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"];  
  34.     [_request startSynchronous];  
  35.       
  36.     //显示网络请求信息在status bar上  
  37.     [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:YES];  
  38.       
  39.     if (_request)  
  40.     {  
  41.         if ([_request error])  
  42.         {  
  43.             NSLog(@"error");  
  44.         }  
  45.         else if ([_request responseString])  
  46.         {  
  47.             NSString *result = [_request responseString];  
  48. //            NSLog(@"%@",result);  
  49.             NSDictionary *mydict = [result JSONValue];  
  50.               
  51.             describeDictionary(mydict);  
  52.         }  
  53.     }  
  54.     else  
  55.     {  
  56.         NSLog(@"request is nil.");  
  57.     }  
  58. }  
  59.   
  60. void describeDictionary(NSDictionary *dict)  
  61. {  
  62.     NSArray *keys;  
  63.     int i, count;  
  64.     id key, value;  
  65.       
  66.     keys = [dict allKeys];  
  67.     count = [keys count];  
  68.     for (i = 0; i 
  69.     {  
  70.         key = [keys objectAtIndex: i];  
  71.         value = [dict objectForKey: key];  
  72.         NSLog (@"Key: %@ for value: %@", key, value);  
  73.     }  
  74. }  
  75.   
  76. @end  


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

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

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!