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

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

//使用AFN请问无参方法

  1. //使用AFN无参
  2. -(void)AfnDemo
  3. {
  4.  
  5. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://115.231.54.166:9090/JobRecordAPP.asmx/QueryWeather"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0f];
  6. AFHTTPRequestOperation *operation =[[AFHTTPRequestOperation alloc] initWithRequest:request];
  7. [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  8. //返回字符串
  9. NSString *html = operation.responseString;
  10. NSLog(@"html----%@",html);
  11. //将返回字符串头去掉:<?xml version=\"1.0\" encoding=\"utf-8\"?>
  12. NSString *str =@"<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  13. NSString *strhtml =[html stringByReplacingOccurrencesOfString:str withString:@""];
  14. //将返回字符串头去掉
  15. strhtml = [strhtml stringByReplacingOccurrencesOfString:@"<string xmlns=\"http://tempuri.org/\">" withString:@""];
  16. //将返回的字符串尾去掉
  17. strhtml = [strhtml stringByReplacingOccurrencesOfString:@"</string>" withString:@""];
  18. //去掉结尾空格
  19. NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
  20. strhtml= [[NSString alloc]initWithString:[strhtml stringByTrimmingCharactersInSet:whiteSpace]];
  21. NSLog(@"无参----%@",strhtml);
  22.  
  23. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  24. [self initFailure];
  25. }];
  26. [operation start];
  27. }
  1. //使用AFN有参
  2. -(void)AfnDemo2:(NSString *)catObj
  3. {
  4.  
  5. NSString *url = @"http://115.231.54.166:9090/JobRecordAPP.asmx/BrowseStatistics?";
    //设置参数
  6. NSString *k_initUrl3 =[url stringByAppendingFormat:@"LoginID=%@",catObj];
  7. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:k_initUrl3] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0f];
  8. AFHTTPRequestOperation *operation =[[AFHTTPRequestOperation alloc] initWithRequest:request];
  9. [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  10. NSString *html = operation.responseString;
  11. //将返回字符串头去掉:<?xml version=\"1.0\" encoding=\"utf-8\"?>
  12. NSString *str =@"<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  13. NSString *strhtml =[html stringByReplacingOccurrencesOfString:str withString:@""];
  14. //将返回字符串头去掉
  15. strhtml = [strhtml stringByReplacingOccurrencesOfString:@"<string xmlns=\"http://tempuri.org/\">" withString:@""];
  16. //将返回的字符串尾去掉
  17. strhtml = [strhtml stringByReplacingOccurrencesOfString:@"</string>" withString:@""];
  18. //去掉结尾空格
  19. NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
  20. strhtml= [[NSString alloc]initWithString:[strhtml stringByTrimmingCharactersInSet:whiteSpace]];
  21. NSLog(@"有参----%@",strhtml);
  22.  
  23. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  24. [self initFailure];
  25. }];
  26. [operation start];
  27. }
  1.  

  1. 在使用.Net Web服务编写WebService,如上图我们可以看到返回的数据,前后数据多出了
  1. <?xml version=\"1.0\" encoding=\"utf-8\"?>
  1. <string xmlns=\"http://tempuri.org/\">
  1. </string>
    所以在得到json格式的数据的时候,先把前后多余的数据给替换了设置为空。
    完整案例数据解析过程 NSString->NSData->NSarray
  1. //从
  2. -(void)initSetting
  3. {
  4. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://115.231.54.166:9090/JobRecordAPP.asmx/QueryWeather"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0f];
  5. AFHTTPRequestOperation *operation =[[AFHTTPRequestOperation alloc] initWithRequest:request];
  6. [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  7. NSString *html = operation.responseString;
  8. //将返回字符串头去掉:<?xml version=\"1.0\" encoding=\"utf-8\"?>
  9. NSString *str =@"<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  10. NSString *strhtml =[html stringByReplacingOccurrencesOfString:str withString:@""];
  11. //将返回字符串头去掉
  12. strhtml = [strhtml stringByReplacingOccurrencesOfString:@"<string xmlns=\"http://tempuri.org/\">" withString:@""];
  13. //将返回的字符串尾去掉
  14. strhtml = [strhtml stringByReplacingOccurrencesOfString:@"</string>" withString:@""];
  15. //去掉结尾空格
  16. NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
  17. strhtml= [[NSString alloc]initWithString:[strhtml stringByTrimmingCharactersInSet:whiteSpace]];
  18.  
  19. NSData *data= [strhtml dataUsingEncoding:NSUTF8StringEncoding];
  20.  
  21. [[ProblemPaperKindObject share] videoWithDict:[data JSONValue]];
  22.  
  23. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  24. [self initFailure];
  25. }];
  26. [operation start];
  27.  
  28. }
  1. ProblemPaperKindObject 课程分类模型类
  2.  
  3. ProblemPaperKindObject.h
  4.  
  5. #import <Foundation/Foundation.h>
  6.  
  7. @interface ProblemPaperKindObject : NSObject
  8. /*!
  9. * 分类信息
  10. */
  11. @property(nonatomic,strong) NSArray *categorys;
  12. /*!
  13. * 显示的分类
  14. */
  15. @property(nonatomic,strong) NSArray *categorysShow;
  16. @property(nonatomic,strong) NSArray *categoryHide;
  17.  
  18. @property(nonatomic,strong) NSArray *indexsCategorys;
  19. //类别编号
  20. @property (assign,nonatomic) int PRKID;
  21.  
  22. //根节点
  23. @property (assign,nonatomic) int ParentID;
  24.  
  25. //名称
  26. @property (copy,nonatomic) NSString *Name;
  27.  
  28. - (void)videoWithDict:(NSDictionary *)dict;
  29. - (void)videoWithDict2:(NSDictionary *)dict;
  30. - (void)initWithJson:(NSDictionary *)dict;
  31.  
  32. +(ProblemPaperKindObject *)share;
  33.  
  34. @end
  1. #import "ProblemPaperKindObject.h"
  2. #import "Common.h"
  3. #import "Config.h"
  4. @implementation ProblemPaperKindObject
  5. +(ProblemPaperKindObject *)share
  6. {
  7. static ProblemPaperKindObject * _ProblemPaperKindObject_Share=nil;
  8. static dispatch_once_t onceToken;
  9. dispatch_once(&onceToken, ^{
  10. _ProblemPaperKindObject_Share=[[ProblemPaperKindObject alloc] init];
  11. });
  12. return _ProblemPaperKindObject_Share;
  13. }
  14. -(void)videoWithDict:(NSArray *)dict
  15. {
  16. NSMutableArray *categorysTemp=[[NSMutableArray alloc] init];
  17. for (NSDictionary *dic in dict) {
  18. // NSLog(@"videoWithDict---%@",dic[@"Name"]);
  19. ProblemPaperKindObject *video = [[ProblemPaperKindObject alloc] init];
  20. video.PRKID = [dic[@"PRKID"] intValue];
  21. video.ParentID = [dic[@"ParentID"] intValue];
  22. video.Name = dic[@"Name"];
  23. [categorysTemp addObject:video];
  24. }
  25. self.indexsCategorys=[NSArray arrayWithArray:categorysTemp];
  26.  
  27. // [video setValuesForKeysWithDictionary:dict]; // KVC方法使用前提: 字典中的所有key 都能在 模型属性 中找到
  28.  
  29. }
  30. -(void)videoWithDict2:(NSArray *)dict
  31. {
  32. //分类中分类!
  33.  
  34. NSMutableArray *categorysTemp=[[NSMutableArray alloc] init];
  35. for (NSDictionary *dic in dict) {
  36. ProblemPaperKindObject *video = [[ProblemPaperKindObject alloc] init];
  37. video.PRKID = [dic[@"PRKID"] intValue];
  38. video.ParentID = [dic[@"ParentID"] intValue];
  39. video.Name = dic[@"Name"];
  40. [categorysTemp addObject:video];
  41. //NSLog(@"PRKID---%@",[dic objectForKey:@"PRKID"]);
  42.  
  43. }
  44. self.categorys=[NSArray arrayWithArray:categorysTemp];
  45. //显示的分类
  46. NSArray *categoryShowArr=[[Common readLocalString:k_categoryShowPath secondPath:k_categoryShowPath2] JSONValue];
  47. // NSLog(@"k_DocumentsPath---%@",k_DocumentsPath);
  48. // NSLog(@"categoryShowArr----%@",categoryShowArr);
  49. //
  50. // NSLog(@"categoryShowArr--%@",categoryShowArr);
  51.  
  52. NSMutableArray *showTempArr=[[NSMutableArray alloc] init];
  53. for (int i=; i<categoryShowArr.count; i++) {
  54. NSPredicate *filter=[NSPredicate predicateWithFormat:@"PRKID=%@",[categoryShowArr objectAtIndex:i]];
  55. [showTempArr addObjectsFromArray:[self.categorys filteredArrayUsingPredicate:filter]];
  56. }
  57. self.categorysShow=[NSArray arrayWithArray:showTempArr];
  58. // NSLog(@"categorysShow--%@",self.categorysShow);
  59. NSPredicate *filter2=[NSPredicate predicateWithFormat:@" NOT (PRKID in %@)",categoryShowArr];
  60. self.categoryHide=[self.categorys filteredArrayUsingPredicate:filter2];
  61.  
  62. // [video setValuesForKeysWithDictionary:dict]; // KVC方法使用前提: 字典中的所有key 都能在 模型属性 中找到
  63.  
  64. }
  65.  
  66. -(void)initWithJson:(NSDictionary *)dict
  67. {
  68. [self videoWithDict:dict];
  69. }
  70. @end
  1. JSON.h
  2.  
  3. //
  4. // JSON1.h
  5. // NewsBrowser
  6. //
  7. // Created by Ethan on 13-11-17.
  8. // Copyright (c) 2013年 Ethan. All rights reserved.
  9. //
  10.  
  11. #import <Foundation/Foundation.h>
  12.  
  13. @interface NSString (JSON)
  14. - (id) JSONValue;
  15. @end
  16.  
  17. @interface NSData (JSON)
  18. - (id) JSONValue;
  19. @end
  20.  
  21. @interface NSObject (JSON)
  22. - (NSString *)JSONRepresentation;
  23. @end
  24.  
  25. JSON.m
  26.  
  27. //
  28. // JSON1.m
  29. // NewsBrowser
  30. //
  31. // Created by Ethan on 13-11-17.
  32. // Copyright (c) 2013年 Ethan. All rights reserved.
  33. //
  34.  
  35. #import "JSON.h"
  36.  
  37. @implementation NSString (JSON)
  38. - (id) JSONValue {
  39. NSError *error = nil;
  40. id obj = [NSJSONSerialization JSONObjectWithData:[self dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
  41. if (error)
  42. NSLog(@"%@", [error description]);
  43. return obj;
  44. }
  45. @end
  46.  
  47. @implementation NSData (JSON)
  48. - (id) JSONValue {
  49. NSError *error = nil;
  50. id obj = [NSJSONSerialization JSONObjectWithData:self options:NSJSONReadingMutableContainers error:&error];
  51. if (error)
  52. NSLog(@"%@", [error description]);
  53. return obj;
  54. }
  55. @end
  56.  
  57. @implementation NSObject (JSON)
  58. - (NSString *)JSONRepresentation
  59. {
  60. if ([NSJSONSerialization isValidJSONObject:self]) {
  61. NSError *error = nil;
  62. NSData *data=[NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
  63. if (error)
  64. NSLog(@"%@", [error description]);
  65. NSString *result=[[NSString alloc]initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
  66. return result;
  67. }
  68. return @"";
  69. }
  70. @end

AFNetworking请求WebService Demo下载地址:链接: http://pan.baidu.com/s/1c0i9fS8 密码: 2vt9

iOS网络开发-AFNetworking请求asp.net WebService的更多相关文章

  1. iOS网络_优化请求性能

    iOS网络_优化请求性能 一,度量网络性能 1,网络带宽 用于描述无线网络性能的最常见度量指标就是带宽.在数字无线通信中,网络带宽可以 描述为两个端点之间的通信通道每秒钟可以传输的位数.现代无线网络所 ...

  2. iOS开发之结合asp.net webservice实现文件上传下载

    iOS开发中会经常用到文件上传下载的功能,这篇文件将介绍一下使用asp.net webservice实现文件上传下载. 首先,让我们看下文件下载. 这里我们下载cnblogs上的一个zip文件.使用N ...

  3. IOS网络开发概述

    概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博.微信等,这些应用本身可能采用iOS开发,但是所有的数据支撑都是基于后台网络服务器的.如今,网络编程越来越普遍,孤立的应用通常是没有生命力 ...

  4. iOS开发之iPhone通过get和post方式请求asp.net webservice

    .创建一个webservice .在webconfig中启用http get 和http post. 复制代码 <</span> webServices > <</ ...

  5. iOS网络开发—POST请求和GET请求

    创建GET请求: // 1.设置请求路径 NSString *urlStr=[NSString stringWithFormat:@"http://192.168.1.53:8080/MJS ...

  6. IOS网络开发(三)

    1 飞机航班查询软件 1.1 问题 NSURLConnection是IOS提供的用于处理Http协议的网络请求的类,可以实现同步请求也可以实现异步请求,本案例使用NSURLConnection类实现一 ...

  7. IOS网络开发实战(二)

      1 飞机航班查询软件 1.1 问题 NSURLConnection是IOS提供的用于处理Http协议的网络请求的类,可以实现同步请求也可以实现异步请求,本案例使用NSURLConnection类实 ...

  8. iOS网络之数据请求GET和POST

    1. HTTP和HTTPS协议 1> URL URL全称是Uniform Resource Locator(统一资源定位符)通过1个URL,能找到互联网上唯一的1个资源 URL就是资源的地址.位 ...

  9. IOS网络编程之请求内容

    资料均来自互联网,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任. 人魔七七:http://www.cnblogs.com/qiqibo/ 一个http请求只要由三 ...

随机推荐

  1. elk之nginx

    elk之nginx: ignore_older => 86400,不处理一天以前的文件. zjtest7-frontend:/usr/local/logstash-2.3.4/config# c ...

  2. SymPy-符号运算好帮手

    SymPy-符号运算好帮手 SymPy是Python的数学符号计算库,用它可以进行数学公式的符号推导.为了调用方便,下面所有的实例程序都假设事先从sympy库导入了所有内容: >>> ...

  3. oracle定时执行计划任务

    show parameter job_queue_processes; alter system set job_queue_processes=10; 1,创建测试表 create table jo ...

  4. c#搭建服务端 简单中最高效的数据操作Linq (4)

    .NET F 3.5之后提出的linq to sql 概念,大大的简化了对于数据对象的操作,可以通过简单的语法直接操作数据对象,如List,Linq to sql类 等等. 1.使用Linq to s ...

  5. kafka学习(三)-kafka集群搭建

    kafka集群搭建 下面简单的介绍一下kafka的集群搭建,单个kafka的安装更简单,下面以集群搭建为例子. 我们设置并部署有三个节点的 kafka 集合体,必须在每个节点上遵循下面的步骤来启动 k ...

  6. python 【第四篇】:面向对象(一)

    1.前言 提笔忘字,感慨良多!python自习前前后后有一年多了吧,貌似花了不少时间,其实没学到啥东西,都是在面向对象编程之前基础知识这块一直打转转,每次到了面向对象这块就感觉很蒙,看两天直接放弃,从 ...

  7. favicon

    <link rel="icon" href="Images/wangyi.ico" type="text/plain" />

  8. B. Wet Shark and Bishops(思维)

    B. Wet Shark and Bishops time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. UNIX网络编程卷1 时间获取程序server UDP 协议无关

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie /** * UDP 协议无关 调用 getaddrinfo 和 udp_server **/ ...

  10. eclipse使用技巧---使用正则表达式查找替换

    1,Eclipse ctrl+f 打开查找框2,选中 Regular expressions (正则表达式) 去掉/* */(eclipse)        /\*(.|[\r\n])*?\*/去掉/ ...