美文网首页
利用抓取的快递100接口实现的快递查询功能

利用抓取的快递100接口实现的快递查询功能

作者: 不规则先生 | 来源:发表于2016-12-22 11:09 被阅读705次

@property (strong,nonatomic) UITextField *showContextView;

//接受相关结果的字符串

@property (nonatomic,strong)NSMutableString *result;

@property (nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation ExpressViewController{

UITableView *contentTable;

}

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

self.dataArr = [[NSMutableArray alloc]init];

[self setXFView];

[self setView];

//

//    [self loadMoreTopics];

// Do any additional setup after loading the view.

}

-(void)setView{

UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];

self.showContextView = [[UITextField alloc]initWithFrame:CGRectMake(10,0, ScreenWidth-20, 30)];

self.showContextView.keyboardType = UIKeyboardTypeNumberPad;

self.showContextView.placeholder = @"请输入单号";

self.showContextView.text = @"3320426143552";

[self.showContextView setFont:[UIFont fontWithName:@"Arial" size:23]] ;

self.showContextView.textColor = [UIColor blackColor];

self.showContextView.backgroundColor = textBackGroundColor;

self.showContextView.delegate = self;

[headView addSubview:self.showContextView];

UIButton *search = [UIButton addBtnImage:@"" AndFrame:CGRectMake(ScreenWidth-110, getExpressBtn.frame.origin.y, 100, 30) WithTitle:@"查找" AndTitleColor:textdeepgrayColor AndTitleFont:18 AndImgInsets:UIEdgeInsetsZero AndTitleEdgeInsets:UIEdgeInsetsZero AndTarget:self AndAction:@selector(loadMoreTopics)];

[headView addSubview:search];

headView.frame = CGRectMake(0, 0, ScreenWidth, frameH(search, 10)) ;

contentTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 10, ScreenWidth, ScreenHeight-frameH(search, 10)) style:UITableViewStylePlain];

contentTable.dataSource = self;

contentTable.delegate = self;

contentTable.separatorStyle = UITableViewCellSeparatorStyleNone;

contentTable.tableHeaderView = headView;

[self.view addSubview:contentTable];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.dataArr.count;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 100;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

NSString *tag            =[NSString stringWithFormat:@"%ldtag",(long)indexPath.row];

//        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tag];

NSDictionary *dic = [self.dataArr objectAtIndex:indexPath.row];

//            if (cell == nil) {

UITableViewCell* cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tag];

//            }

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];

UILabel *time = [UILabel addLabelWithFrame:CGRectMake(0, 5, 90, 100) AndText:[dic valueForKey:@"time"] AndFont:16 AndAlpha:1 AndColor:[UIColor blackColor]];

[time setTextAlignment:NSTextAlignmentCenter];

[view addSubview:time];

time.numberOfLines = 0;

Phonelabel *content;

UILabel *content1;

NSString *contentText = [dic valueForKey:@"context"];

if([contentText containsString:@"派件"])

{

content =[[Phonelabel alloc] initWithFrame:CGRectMake(140, 10, ScreenWidth-145, 90)];

content.centerY = time.centerY;

[content setFont:[UIFont fontWithName:@"Arial" size:18]];

NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:contentText];

if (indexPath.row == 0) content.color = [UIColor redColor];

else  content.color = [UIColor blackColor];

[content setLinkText:attStr];

[view addSubview:content];

}else{

content1 = [[UILabel alloc]initWithFrame:CGRectMake(140, 0, ScreenWidth-145, 100)];

content1.numberOfLines = 0;

content1.text = [dic valueForKey:@"context"];

[view addSubview:content1];

}

NSLog(@"%lu,%ld",(unsigned long)self.dataArr.count,(long)indexPath.row);

if (indexPath.row == 0) {

[time setTextColor:textBlueColor];

[content setTextColor:textBlueColor];

}

float y;

if (content) {

y = content.centerY;

}else{

y = content1.centerY;

}

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(frameW(time, 10), y - 15, 30, 30)];

imageView.cornerRadius = 15;

if ([contentText containsString:@"派件"]) {

imageView.image = [UIImage imageNamed:@"icon-people"];

}else if([contentText containsString:@"签收"]) {

imageView.image = [UIImage imageNamed:@"icon_ok"];

}else {

imageView.image = [UIImage imageNamed:@"icon-yunshu"];

}

[view addSubview:imageView];

[cell addSubview:view];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell;

}

-(void)beginRecongnize{

[self startListenning];

}

-(void)loadMoreTopics{

[self.showContextView resignFirstResponder];

if (![self.showContextView.text isPureInt:self.showContextView.text ]) {

[SVProgressHUD showErrorWithStatus:@"请输入正确单号"];

return;

}

[SVProgressHUD showWithStatus:@"正在努力加载中!"];

//    http://www.kuaidi100.com/autonumber/autoComNum?text=8821921

//    NSDictionary *dic1 = @{};

NSString *url1 =[NSString stringWithFormat:@"%@%@",ExpressCAPI,self.showContextView.text];

[HttpTool post:url1  params:nil success:^(id json){

//        int status = [[json valueForKey:@"status"] intValue] ;

[SVProgressHUD dismiss];

NSArray *array = [json valueForKey:@"auto"];

if (array.count<1) {

[SVProgressHUD showErrorWithStatus:@"单号错误,请重新填写!"];

return ;

}

NSDictionary *dic = array[0];

NSString *comCode = [dic valueForKey:@"comCode"];

[SVProgressHUD dismiss];

[self getStatue:comCode];

}failure:^(NSError *error){

NSLog(@"%@",error);

[SVProgressHUD showErrorWithStatus:@"网络请求失败"];

//        [SVProgressHUD dismiss];

}];

}

//com 物流公司编号

//nu 物流单号

//time 每条跟踪信息的时间

//context 每条跟综信息的描述

//state 快递单当前的状态 :

//0:在途,即货物处于运输过程中;

//1:揽件,货物已由快递公司揽收并且产生了第一条跟踪信息;

//2:疑难,货物寄送过程出了问题;

//3:签收,收件人已签收;

//4:退签,即货物由于用户拒签、超区等原因退回,而且发件人已经签收;

//5:派件,即快递正在进行同城派件;

//6:退回,货物正处于退回发件人的途中;

//该状态还在不断完善中,若您有更多的参数需求,欢迎发邮件至 kuaidi@kingdee.com 提出。

//status 查询结果状态:

//0:物流单暂无结果,

//1:查询成功,

//2:接口出现异常,

//message 无意义,请忽略

//condition 无意义,请忽略

//ischeck 无意义,请忽略

-(void)getStatue:(NSString *)comCode{

NSString *url =[NSString stringWithFormat:@"http://www.kuaidi100.com/query?type=%@&postid=%@",comCode,self.showContextView.text];

[HttpTool post:url  params:nil success:^(id json){

[SVProgressHUD dismiss];

int status = [[json valueForKey:@"status"] intValue];

if (status) {

switch (status) {

case 200:

{self.dataArr = [json valueForKey:@"data"];

[contentTable reloadData];

break;

}

case 201:

[SVProgressHUD showSuccessWithStatus:[json valueForKey:@"message"]];

break;

default:

[SVProgressHUD showErrorWithStatus:[json valueForKey:@"message"]];

break;

}

}else{

[SVProgressHUD showErrorWithStatus:@"查询失败!"];

}

}failure:^(NSError *error){

NSLog(@"%@",error);

[SVProgressHUD showErrorWithStatus:@"网络请求失败"];

//        [SVProgressHUD dismiss];

}];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

//    回收键盘,取消第一响应者

[textField resignFirstResponder]; return YES;

}

@end

相关文章

网友评论

      本文标题:利用抓取的快递100接口实现的快递查询功能

      本文链接:https://www.haomeiwen.com/subject/paicvttx.html