搜尋
首頁php教程php手册iOS通过ASIHttpRequest接收php端发送的Json数据

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数据

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。