ASIHTTPRequest GET(下载) POST(上传)

ASIHTTPRequest   GET(下载) POST(上传)

官网 :http://allseeing-i.com/ASIHTTPRequest/


如何让ASIHttpRequest支持ARC:

1.导入FrameWork:CFNetwork.framework  SystemConfiguration.framework  

MobileCoreServices.framework 

CoreGraphics.framework 

libz.dylib


2.Targets - Build Phases - 八个ASI*.m和Reachabillity.m - 双击弹出对话框,在对话框中输入“-fno-objc-arc”

再clean,build就能成功!




#import "ASIHttpRequest.h"

#import "ASIFormDataRequest.h"


GET: 

XmlLogin *user = [Share getUser];

        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@?Token=%@&chatid=%d&userid=%d",GETCHATMESSAGEHISTORY,[Tools getToken],self.currentChat.iD,user.iD]];

        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  

        [request setRequestMethod:@"GET"];

        [request startSynchronous];   //同步 

        //int a = [request responseStatusCode];

        

        NSError *err = [request error];

        if(!err)

        {

            NSString *response = [request responseString]; 

            NSLog(@"%@",response);  //这里输出

            

            SBJsonParser * parser = [[SBJsonParser alloc] init];

            NSMutableDictionary *jsonDic = [parser objectWithString:response error:&err];

            NSMutableDictionary * dicUserInfo = [jsonDic objectForKey:@"ChatMessage"];

            NSMutableArray *arrayHis = [[[NSMutableArray alloc] init] autorelease];

            for(NSDictionary *dictionary in dicUserInfo)

            {

                MessageLine *xmlparser = [[MessageLine alloc]init];

                xmlparser.userId = [[dictionary objectForKey:@"UserID"] intValue];

                xmlparser.message = [dictionary objectForKey:@"Message"];

                xmlparser.userName = [dictionary objectForKey:@"UserName"];

                xmlparser.type = [dictionary objectForKey:@"Type"];

                if([dictionary objectForKey:@"Uri"]!= [NSNull null])

                {

                    xmlparser.uri = [dictionary objectForKey:@"Uri"];

                }

                xmlparser.chatId = [[dictionary objectForKey:@"ChatID"] intValue];

                xmlparser.messageId = [[dictionary objectForKey:@"MesssageID"] intValue];

                xmlparser.timeStamp = [dictionary objectForKey:@"Timestamp"];

                [arrayHis addObject:xmlparser];

                [xmlparser release];

            }

            [parser release];

    }




POST:


XmlLogin *user = [Share getUser];

        //POST

        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",SENDMESSAGE]];

        //self.requestAll = [ASIHTTPRequest requestWithURL:url];

       ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url];

        

        [request1 addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];

        [request1 setRequestMethod:@"POST"];

        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

        [request setPostValue:[NSString stringWithFormat:@"%@",[Tools getToken]] forKey:@"Token"];

        [request addPostValue:[NSString stringWithFormat:@"%d",self.currentChat.iD] forKey:@"chatid"];

        [request addPostValue:[NSString stringWithFormat:@"%d",user.iD] forKey:@"userid"];

        if(message!=nil)

        {

             [request addPostValue:[NSString stringWithFormat:@"%@",message] forKey:@"message"];

        }

        

        if(self.upImage!=nil)//传的图片

        {

            NSString *fileName = [Tools getDoucementDirectoryFilePathWithFileName:UPIMAGENAME];

            [request setFile:fileName forKey:@"File"];

        }

       // [request setTimeOutSeconds:10];

        //发送消息 此处使用异步(未标注都为同步)

        [request setDelegate:self]; //异步方法

        [request startSynchronous];



//异步需要实现的delegate


#pragma 异步ASIHTTPREQUEST delegate only sendMessage

- (void)requestFinished:(ASIHTTPRequest *)request

{

   

        NSString *response = [request responseString];

        NSLog(@"%@",response);

 }


- (void)requestFailed:(ASIHTTPRequest *)request

{

    NSError *error = [request error];

    NSLog(@"%@",error);

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"网络未请求成功!是否重试?" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES",nil];

    [alert show];

    [alert release];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值