接口网址:http://123.57.75.80:8001/doc/api.php?m=api_login
具体步骤如下:
第一步:准备好需要的框架
(1)SBJson框架:用来解析json串 即解析json格式的XML文件
(2)ASIHttpRequest框架:以get方式发送变量 或以post形式将一个表单数据即一个字典转化成json串封装在NSData数据块中发送数据请求
(3)MBProgressHuD框架:用于数据缓冲图标,表示数据还未发送过来
(4)MD5框架:用于给指定的字符串进行加密。这是与照服务器端解密方式相对应的
第二步:导入框架
注意:先导入ASIHttpRequest框架 设置好之后再把别的框架拉进工程
第三步:新建pch文件把所有框架的头文件导入语句写在里面,以便整个工程使用框架中的类,并按照上篇方法进行配置,内容如下:
//
// PrefixHeader.pch
// ASIHttp2
//
// Created by apple on 15/9/19.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#ifndef ASIHttp2_PrefixHeader_pch
#define ASIHttp2_PrefixHeader_pch
#import "MD5Util.h"
#import "ASIHTTPRequest.h"
#import "SBJson.h"
#import "ASIHTTPRequestDelegate.h"
#import "MBProgressHUD.h"
#endif
开始正式代码编辑:
让当前的控制器遵守ASIhttpRequestDelegate协议 以便执行ASIHttpRequest类的回调方法 一般用两个方法,即结束请求 - (void)requestFinished:(ASIHTTPRequest *)request
和请求失败后的一个方法:
- (void)requestFailed:(ASIHTTPRequest *)request
编辑控制器的.h文件如下:
//
// ViewController.h
// ASIHttp2
//
// Created by apple on 15/9/19.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<ASIHTTPRequestDelegate>
@end
编辑控制器的.m文件如下:
//
// ViewController.m
// ASIHttp2
//
// Created by apple on 15/9/19.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loginPost];
}
-(NSString *) currentTime:(NSDate *) currentDate
{ //加密时把指定的时间都加了31秒
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSTimeInterval seconds = (long) [currentDate timeIntervalSince1970]+31; // currentDate据1970年的秒数加上31秒
NSDate *dt = [NSDate dateWithTimeIntervalSince1970:seconds]; // 将距离1970年seconds秒的时间转化为NSdate
return [formatter stringFromDate:dt];
}
// 获取当前的时间,并转化为指定格式的字符
-(NSString *) currentTime1:(NSDate *) currentDate
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
return [formatter stringFromDate:currentDate];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
[MBProgressHUD hideAllHUDsForView:self.view animated:YES] ;
NSLog(@"%@",request.responseString);
NSDictionary *dic = [request.responseString JSONValue];
NSDictionary *dataDic = [dic objectForKey:@"data"];
NSString *imgNam = [dataDic objectForKey:@"avatars"]; // 获取图片对应网址
UIImageView *imgv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
imgv.backgroundColor = [UIColor yellowColor];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgNam]];
imgv.image = [UIImage imageWithData:data];
[self.view addSubview:imgv];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSLog(@"请求失败");
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
NSLog(@"1111111");
}
-(void)loginPost
{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"加载中";
NSDate *currentDate = [NSDate date];
NSDictionary *dic = [NSMutableDictionary dictionary];
[dic setValue:@"login" forKey:@"requestcommand"];
[dic setValue:@"name" forKey:@"requestname"];
NSString *password = [MD5Util md5:[NSString stringWithFormat:@"loginname%@",[self currentTime:currentDate]]];
[dic setValue:password forKey:@"requestpassword"];
[dic setValue:[self currentTime1:currentDate] forKey:@"requesttime"];
[dic setValue:@"wlbcs1" forKey:@"account"];
[dic setValue:@"1234567" forKey:@"password"];
// 具体创建请求对象的过程
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://www.dcjyxwzx.cn/api?"]];
request.delegate = self;
[request setRequestMethod:@"POST"];
NSString *paramString = [dic JSONRepresentation]; // 将字典转换成一个json串
NSLog(@"发送的json串:%@",paramString);
[request appendPostData:[paramString dataUsingEncoding:NSUTF8StringEncoding]];
// [request startSynchronous]; // 同步发送
[request startAsynchronous]; //开始异步执行
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
运行结果如下:
后台输出如下: