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

来源:互联网 发布:公司宿舍网络不稳定 编辑:程序博客网 时间:2024/05/06 12:54

在blog文

iOS使用ASIHttpRequest+Json与服务器段脚本进行登陆验证

中,没有仔细的说清楚,到底是如何交互的,很是抱歉;毕竟我用php也仅限于这几天。。


先来看,我刚才写的一个php端:

<?php$arr;    function traverse($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;            }            else if(is_dir($sub_dir))             {    //如果是目录,进行递归            //    echo 'Directory ' . $file . ':<br>';                $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 . '<br>';            $file_arr[$file_index] = $path . '\\' . $file . '<br>';            $file_index++;            }        };                $arr["dir_count"] = count($directory_arr);    //    print_r($file_arr);    //    print_r(count($file_arr));   //     echo '<br>';    //    echo "==============================";    //    echo '<br>';                // 有一个是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脚本之后得到的页面如下:



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

这是其中一个day1的内容:



在对应的iOS端,这样写:

////  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");        }        else if ([_request responseString])        {            NSString *result = [_request responseString];//            NSLog(@"%@",result);            NSDictionary *mydict = [result JSONValue];                        describeDictionary(mydict);        }    }    else    {        NSLog(@"request is nil.");    }}void describeDictionary(NSDictionary *dict){    NSArray *keys;    int i, count;    id key, value;        keys = [dict allKeys];    count = [keys count];    for (i = 0; i < count; i++)    {        key = [keys objectAtIndex: i];        value = [dict objectForKey: key];        NSLog (@"Key: %@ for value: %@", key, value);    }}@end


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




原创粉丝点击