IOS receives Json data sent by php through ASIHttpRequest

Source: Internet
Author: User
Blog. csdn. netzengraoliarticledetails12918369 in the blog text iOS use ASIHttpRequestJson for login verification with the server segment script. I am sorry for not making it clear how it actually interacts; after all, I only use php for these days .. First, let's take a look at the php client I just wrote: [php] viewp

Http://blog.csdn.net/zengraoli/article/details/12918369 in the blog iOS use ASIHttpRequestJson and server segment script login verification, not carefully said clearly, in the end is how to interact, I am very sorry; after all, I only use php for these days .. First, let's look at a php client I just wrote: [php] view p

Http://blog.csdn.net/zengraoli/article/details/12918369


In the blog

IOS uses ASIHttpRequest + Json to perform login verification with the server segment script. I am sorry for not carefully explaining how the script interacts. After all, I only use php for the past few days ..


First, let's look at a php client I just wrote:

[Php]View plaincopyprint?

  1. $ Arr;
  2. Function traverse ($ path = '.')
  3. {
  4. $ Current_dir = opendir ($ path); // opendir () returns a directory handle. If it fails, false is returned.
  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 () returns an entry in the Open Directory handle.
  13. $ Sub_dir = $ path. DIRECTORY_SEPARATOR. $ file; // build the subdirectory path
  14. If ($ file = '.' | $ file = '..')
  15. {
  16. Continue;
  17. }
  18. Else if (is_dir ($ sub_dir ))
  19. {// Perform recursion for directories
  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. {// If it is a file, direct output
  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. // One of them is a titleworkflow first, and the other half is a. 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. ?>



The following page is displayed after the php script is directly run on the server:



This is a demo of getting the current webrootdirectory, the number of directories in the imagesfolder, and the number of .jpg files.

Here is the content of day1:



On the corresponding iOS end, write as follows:

[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. // Display the network request information on the status bar
  27. [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator: YES];
  28. If (_ request)
  29. {
  30. If ([_ request error])
  31. {
  32. NSLog (@ "error ");
  33. }
  34. Else if ([_ 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. Void describeDictionary (NSDictionary * dict)
  48. {
  49. NSArray * keys;
  50. Int I, count;
  51. Id key, value;
  52. Keys = [dict allKeys];
  53. Count = [keys count];
  54. For (I = 0; I <count; I ++)
  55. {
  56. Key = [keys objectAtIndex: I];
  57. Value = [dict objectForKey: key];
  58. NSLog (@ "Key: % @ for value: % @", key, value );
  59. }
  60. }
  61. @ End



This is the result of parsing Json data after calling this Code:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.