看到园子有位朋友需要使用AFN框架请求 WebService,所以就整理了一下,demo下载链接在底部

编写WebService可以看这篇博客 http://www.cnblogs.com/linmingjun/p/4606451.html

//使用AFN请问无参方法

//使用AFN无参
-(void)AfnDemo
{
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://115.231.54.166:9090/JobRecordAPP.asmx/QueryWeather"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0f];
    AFHTTPRequestOperation *operation =[[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        //返回字符串
        NSString *html = operation.responseString;
        NSLog(@"html----%@",html);
        //将返回字符串头去掉:<?xml version=\"1.0\" encoding=\"utf-8\"?>
        NSString *str =@"<?xml version=\"1.0\" encoding=\"utf-8\"?>";
        NSString *strhtml =[html stringByReplacingOccurrencesOfString:str withString:@""];
        //将返回字符串头去掉
        strhtml = [strhtml stringByReplacingOccurrencesOfString:@"<string xmlns=\"http://tempuri.org/\">" withString:@""];
        //将返回的字符串尾去掉
        strhtml = [strhtml stringByReplacingOccurrencesOfString:@"</string>" withString:@""];
        //去掉结尾空格
        NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        strhtml= [[NSString alloc]initWithString:[strhtml stringByTrimmingCharactersInSet:whiteSpace]];
        NSLog(@"无参----%@",strhtml);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self initFailure];
    }];
    [operation start];
}
//使用AFN有参
-(void)AfnDemo2:(NSString *)catObj
{

    NSString *url = @"http://115.231.54.166:9090/JobRecordAPP.asmx/BrowseStatistics?";
//设置参数 NSString
*k_initUrl3 =[url stringByAppendingFormat:@"LoginID=%@",catObj]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:k_initUrl3] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0f]; AFHTTPRequestOperation *operation =[[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *html = operation.responseString; //将返回字符串头去掉:<?xml version=\"1.0\" encoding=\"utf-8\"?> NSString *str =@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"; NSString *strhtml =[html stringByReplacingOccurrencesOfString:str withString:@""]; //将返回字符串头去掉 strhtml = [strhtml stringByReplacingOccurrencesOfString:@"<string xmlns=\"http://tempuri.org/\">" withString:@""]; //将返回的字符串尾去掉 strhtml = [strhtml stringByReplacingOccurrencesOfString:@"</string>" withString:@""]; //去掉结尾空格 NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; strhtml= [[NSString alloc]initWithString:[strhtml stringByTrimmingCharactersInSet:whiteSpace]]; NSLog(@"有参----%@",strhtml); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { [self initFailure]; }]; [operation start]; }

iOS网络开发-AFNetworking请求asp.net WebService

在使用.Net Web服务编写WebService,如上图我们可以看到返回的数据,前后数据多出了
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<string xmlns=\"http://tempuri.org/\">
</string>
所以在得到json格式的数据的时候,先把前后多余的数据给替换了设置为空。
完整案例数据解析过程 NSString->NSData->NSarray
//
-(void)initSetting
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://115.231.54.166:9090/JobRecordAPP.asmx/QueryWeather"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0f];
    AFHTTPRequestOperation *operation =[[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        //将返回字符串头去掉:<?xml version=\"1.0\" encoding=\"utf-8\"?>
        NSString *str =@"<?xml version=\"1.0\" encoding=\"utf-8\"?>";
        NSString *strhtml =[html stringByReplacingOccurrencesOfString:str withString:@""];
        //将返回字符串头去掉
        strhtml = [strhtml stringByReplacingOccurrencesOfString:@"<string xmlns=\"http://tempuri.org/\">" withString:@""];
        //将返回的字符串尾去掉
        strhtml = [strhtml stringByReplacingOccurrencesOfString:@"</string>" withString:@""];
        //去掉结尾空格
        NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        strhtml= [[NSString alloc]initWithString:[strhtml stringByTrimmingCharactersInSet:whiteSpace]];
       
       NSData *data= [strhtml dataUsingEncoding:NSUTF8StringEncoding];
       
        [[ProblemPaperKindObject share] videoWithDict:[data JSONValue]];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self initFailure];
    }];
    [operation start];

}
View Code

相关文章: