iOS通过ASIHttpRequest接收php端发送的Json数据
2017-03-15 21:18
731 查看
iOS通过ASIHttpRequest接收php端发送的Json数据
2013-10-21 21:53 3975人阅读 评论(0) 收藏 举报分类:
iOS相关(51)
版权声明:本文为博主原创文章,未经博主允许不得转载。
在blog文
iOS使用ASIHttpRequest+Json与服务器段脚本进行登陆验证
中,没有仔细的说清楚,到底是如何交互的,很是抱歉;毕竟我用php也仅限于这几天。。先来看,我刚才写的一个php端:
[php] view
plain copy
print?
<?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端,这样写:
[cpp] view
plain copy
print?
//
// 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数据得到的结果:
php端使用二维数组与iOS进行Json通讯
2013-10-22 09:12 3589人阅读 评论(0) 收藏 举报分类:
iOS相关(51)
版权声明:本文为博主原创文章,未经博主允许不得转载。
在这里曾经说到了,如何用php端和iOS通过Json通讯的:http://blog.csdn.net/zengraoli/article/details/12918369
不过这里有一些不足,因为从php中发送的数据,可以看出,还需要在iOS进行进一步处理,才能得到最终文件的路径
找到了php使用二维数组的资料,写了一小段测试例子(真是新手了),等初步完成之后,再回过来修改这一部分:
[php] view
plain copy
print?
<?php
$arr[0]=array(
"album_cover" => "img3.test.com/p1030872603.jpg",
"album_id" => 49705332,
"album_name" => "\U522b\U4eba\U7684\U4e66\U623f",
"user_id" => 1233832,
"user_name" => "\U72d0\U8760",
"user_picurl" => "img3.test.com/icon/u1233832-146.jpg",
);
$arr[1]=array(
"album_cover" => "img4.test.com/p1880278549.jpg",
"album_id" => 91536010,
"album_name" => "\U65e7\U5ba4\U65b0\U5c45[\U5317\U4eac50\U5e73\U7c73\U5c0f\U5bb6]",
"user_id" => pei830,
"user_name" => PEI,
"user_picurl" => "img3.test.com/icon/ul2341445-326.jpg",
);
$arr[2]=array(
"album_cover" => "img5.test.com/p1880278549.jpg",
"album_id" => 91536010,
"album_name" => "\U65e7\U5ba4\U65b0\U5c45[\U5317\U4eac50\U5e73\U7c73\U5c0f\U5bb6]",
"user_id" => pei830,
"user_name" => PEI,
"user_picurl" => "img3.douban.com/icon/ul2341445-326.jpg",
);
$result = json_encode($arr);
print_r($result);
?>
效果是这样的:
相关文章推荐
- iOS通过ASIHttpRequest接收php端发送的Json数据
- iOS通过ASIHttpRequest接收php端发送的Json数据
- iOS通过ASIHttpRequest接收php端发送的Json数据
- iOS通过ASIHTTPRequest提交JSON数据
- iOS通过ASIHTTPRequest提交JSON数据
- iOS通过ASIHTTPRequest提交JSON数据
- iOS通过ASIHTTPRequest提交JSON数据
- iOS通过ASIHTTPRequest提交JSON数据
- iOS通过ASIHTTPRequest提交JSON数据
- iOS通过ASIHTTPRequest提交JSON数据
- 详解iOS通过ASIHTTPRequest提交JSON数据
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- iOS开发ASIHttpRequest发送数据【3】
- iOS开发asi使用(二)ASIHttpRequest发送数据
- [php]通过http post发送json数据
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- PHP通过Http Post请求发送Json对象数据?
- ios开发之ASIHttpRequest-发送数据
- [php]通过http post发送json数据