看到园子有位朋友需要使用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];
}

在使用.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]; }
ProblemPaperKindObject 课程分类模型类

ProblemPaperKindObject.h

#import <Foundation/Foundation.h>

@interface ProblemPaperKindObject : NSObject
/*!
* 分类信息
*/
@property(nonatomic,strong) NSArray *categorys;
/*!
* 显示的分类
*/
@property(nonatomic,strong) NSArray *categorysShow;
@property(nonatomic,strong) NSArray *categoryHide; @property(nonatomic,strong) NSArray *indexsCategorys;
//类别编号
@property (assign,nonatomic) int PRKID; //根节点
@property (assign,nonatomic) int ParentID; //名称
@property (copy,nonatomic) NSString *Name; - (void)videoWithDict:(NSDictionary *)dict;
- (void)videoWithDict2:(NSDictionary *)dict;
- (void)initWithJson:(NSDictionary *)dict; +(ProblemPaperKindObject *)share; @end
#import "ProblemPaperKindObject.h"
#import "Common.h"
#import "Config.h"
@implementation ProblemPaperKindObject
+(ProblemPaperKindObject *)share
{
static ProblemPaperKindObject * _ProblemPaperKindObject_Share=nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_ProblemPaperKindObject_Share=[[ProblemPaperKindObject alloc] init];
});
return _ProblemPaperKindObject_Share;
}
-(void)videoWithDict:(NSArray *)dict
{
NSMutableArray *categorysTemp=[[NSMutableArray alloc] init];
for (NSDictionary *dic in dict) {
// NSLog(@"videoWithDict---%@",dic[@"Name"]);
ProblemPaperKindObject *video = [[ProblemPaperKindObject alloc] init];
video.PRKID = [dic[@"PRKID"] intValue];
video.ParentID = [dic[@"ParentID"] intValue];
video.Name = dic[@"Name"];
[categorysTemp addObject:video];
}
self.indexsCategorys=[NSArray arrayWithArray:categorysTemp]; // [video setValuesForKeysWithDictionary:dict]; // KVC方法使用前提: 字典中的所有key 都能在 模型属性 中找到 }
-(void)videoWithDict2:(NSArray *)dict
{
//分类中分类! NSMutableArray *categorysTemp=[[NSMutableArray alloc] init];
for (NSDictionary *dic in dict) {
ProblemPaperKindObject *video = [[ProblemPaperKindObject alloc] init];
video.PRKID = [dic[@"PRKID"] intValue];
video.ParentID = [dic[@"ParentID"] intValue];
video.Name = dic[@"Name"];
[categorysTemp addObject:video];
//NSLog(@"PRKID---%@",[dic objectForKey:@"PRKID"]); }
self.categorys=[NSArray arrayWithArray:categorysTemp];
//显示的分类
NSArray *categoryShowArr=[[Common readLocalString:k_categoryShowPath secondPath:k_categoryShowPath2] JSONValue];
// NSLog(@"k_DocumentsPath---%@",k_DocumentsPath);
// NSLog(@"categoryShowArr----%@",categoryShowArr);
//
// NSLog(@"categoryShowArr--%@",categoryShowArr); NSMutableArray *showTempArr=[[NSMutableArray alloc] init];
for (int i=; i<categoryShowArr.count; i++) {
NSPredicate *filter=[NSPredicate predicateWithFormat:@"PRKID=%@",[categoryShowArr objectAtIndex:i]];
[showTempArr addObjectsFromArray:[self.categorys filteredArrayUsingPredicate:filter]];
}
self.categorysShow=[NSArray arrayWithArray:showTempArr];
// NSLog(@"categorysShow--%@",self.categorysShow);
NSPredicate *filter2=[NSPredicate predicateWithFormat:@" NOT (PRKID in %@)",categoryShowArr];
self.categoryHide=[self.categorys filteredArrayUsingPredicate:filter2]; // [video setValuesForKeysWithDictionary:dict]; // KVC方法使用前提: 字典中的所有key 都能在 模型属性 中找到 } -(void)initWithJson:(NSDictionary *)dict
{
[self videoWithDict:dict];
}
@end
JSON.h

//
// JSON1.h
// NewsBrowser
//
// Created by Ethan on 13-11-17.
// Copyright (c) 2013年 Ethan. All rights reserved.
// #import <Foundation/Foundation.h> @interface NSString (JSON)
- (id) JSONValue;
@end @interface NSData (JSON)
- (id) JSONValue;
@end @interface NSObject (JSON)
- (NSString *)JSONRepresentation;
@end JSON.m //
// JSON1.m
// NewsBrowser
//
// Created by Ethan on 13-11-17.
// Copyright (c) 2013年 Ethan. All rights reserved.
// #import "JSON.h" @implementation NSString (JSON)
- (id) JSONValue {
NSError *error = nil;
id obj = [NSJSONSerialization JSONObjectWithData:[self dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
if (error)
NSLog(@"%@", [error description]);
return obj;
}
@end @implementation NSData (JSON)
- (id) JSONValue {
NSError *error = nil;
id obj = [NSJSONSerialization JSONObjectWithData:self options:NSJSONReadingMutableContainers error:&error];
if (error)
NSLog(@"%@", [error description]);
return obj;
}
@end @implementation NSObject (JSON)
- (NSString *)JSONRepresentation
{
if ([NSJSONSerialization isValidJSONObject:self]) {
NSError *error = nil;
NSData *data=[NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
if (error)
NSLog(@"%@", [error description]);
NSString *result=[[NSString alloc]initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
return result;
}
return @"";
}
@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. WebRTC 音视频开发

    WebRTC 音视频开发 webrtc   Android IOS WebRTC 音视频开发总结(七八)-- 为什么WebRTC端到端监控很关键? 摘要: 本文主要介绍WebRTC端到端监控(我们翻译 ...

  2. Unix/Linux环境C编程入门教程(8) FreeBSD CCPP开发环境搭建

    1. FreeBSD是一种自由类Unix操作系统,是由经过BSD.386BSD和4.4BSD发展而来的类Unix的一个重要分支.FreeBSD拥有超过200名活跃开发者和上千名贡献者.FreeBSD被 ...

  3. 一些复数运算的C语言实现

    很久不写博客了.第一次写博客是在04年,最近的一次还是在大学时,在学校时,甚至还有过自己去买虚拟主机搭WordPress写博客的经历.现在工作时间越长,越发现积累的重要性.那么就从这里开始吧,重新开始 ...

  4. MongoDB Error

    ①,org.springframework.core.convert.ConverterNotFoundException: No converter found capable of     con ...

  5. S3C6410嵌入式应用平台构建(四)——linux-3.14.4移植到OK6410-(初步启动)

    这次,还是把基本的基于我目前最新的Linux源码进行移植到OK6410吧,同时也写下我移植过程中遇到的问题及解决方法,不过有些方法是借鉴网上的,有些是自己加的,会有一些小bug. 一.基本工作 1. ...

  6. poj2186 Popular Cows --- 强连通

    给一个有向图,问有多少结点是其它全部结点都能够到达的. 等价于,在一个有向无环图上,找出度为0 的结点.假设出度为0的结点仅仅有一个,那么这个就是答案.假设大于1个.则答案是0. 这题有环.所以先缩点 ...

  7. 请问下mtk双卡手机怎样发短信是怎样选择sim卡来发(双卡都可用的情况下)?

    如题,我如今可以获取双卡状态,当仅仅有单一卡的时候可以指定sim卡进行发短信,可是双卡都可用的情况下,程序就默认使用卡1发短信了.即使指定了sim卡编号.

  8. 如何在Objective-C中实现链式语法?

    在接触到开源项目 Masonry 后,里面的布局约束的链式写法让我颇感兴趣,就像下面这样: 1 2 3 4 5 6 7 8 UIEdgeInsets padding = UIEdgeInsetsMak ...

  9. golang之匿名函数

    package main import "fmt" /* squares返回一个匿名函数 * 该匿名函数每次调用返回下一个数的平方 * func name(parameter-li ...

  10. 指定hive输出格式

    0.11版本以前: sed -e 's/\x01/|/g' file 0.11版本以后: insert overwrite local directory '/opt/aimcpro/libc/tes ...