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];
}