#pragma mark  get country code//同步
-(void)getFKjsonCountryCode
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ), ^{ NSString *urlString = [NSString stringWithFormat:@"http://www.baidu.com"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
//post
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"ios" forHTTPHeaderField:@"X-Channel"];
[request setValue:@"" forHTTPHeaderField:@"X-APP-ID"];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
//NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *countryDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
if ([urlResponse statusCode] >= && [urlResponse statusCode] < )
{
NSMutableArray * countryArr = [[countryDic objectForKey:@"data"]objectForKey:@"phone_codes"];
if(countryArr)
{
for(int i = ; i<[countryArr count]; i++)
{
NSLog(@"countryCode:%@ %@",[[countryArr objectAtIndex:i]objectForKey:@"country"],[[countryArr objectAtIndex:i]objectForKey:@"code"]);
}
}
// NSLog(@"12:%@",[[[[weatherDic objectForKey:@"data"]objectForKey:@"phone_codes"]objectAtIndex:0]objectForKey:@"country"]);
}
else
{ } });
}

#pragma mark  get country code //异步
-(void)getFKjsonCountryCodeAsync
{
NSString *urlString = [NSString stringWithFormat:@"http://www.baidu.com"];
// 初始化请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// 设置
[request setURL:[NSURL URLWithString:urlString]];
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy]; // 设置缓存策略
[request setTimeoutInterval:20.0]; // 设置超时
[request setHTTPMethod:@"POST"];
//...... // receivedData = [[NSMutableData alloc] initData: nil];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection == nil) {
NSLog(@"errors");
// 创建失败
return;
}
} // 收到回应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"receive the response"); }
// 接收数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{ NSError *error = nil;
NSDictionary *countryDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSMutableArray * countryArr = [[countryDic objectForKey:@"data"]objectForKey:@"phone_codes"];
if(countryArr)
{ for(int i = ; i<[countryArr count]; i++)
{
NSLog(@"countryCode:%@ %@",[[countryArr objectAtIndex:i]objectForKey:@"country"],[[countryArr objectAtIndex:i]objectForKey:@"code"]);
}
} }
// 数据接收完毕
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{ NSLog(@"finishLoading");
}
// 返回错误
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{ NSLog(@"Connection failed: %@", error);
}


NSMutableURLRequest Http 请求 同步 异步的更多相关文章

  1. ExtJS4 ajax请求同步异步问题

    今天在写代码过程中遇到一个奇怪的问题.事情是这种,我写了一个简单的页面用来删除选中的用户,请看以下: 由于后台的servlet的代码实现了依据用户名来删除一条记录.所以我在前台的ExtJS代码里面用了 ...

  2. Objective-C Http常用API 同步请求与异步请求

    开发iOS应用要调用Http接口.获取Http资源,有一套比较成熟的框架ASIHTTPRequest.而我还是比较喜欢使用原始一点的 API,而它跟其他的面向对象语言有许多共通之处.本文分同步请求和异 ...

  3. 【Objective-C】Http常用API、同步请求与异步请求[转]

    比较实用, 转载保存 开发iOS应用要调用Http接口.获取Http资源,有一套比较成熟的框架ASIHTTPRequest.而我还是比较喜欢使用原始一点的API,而它跟其他的面向对象语言有许多共通之处 ...

  4. AJAX请求详解 同步异步 GET和POST

    AJAX请求详解 同步异步 GET和POST 上一篇博文(http://www.cnblogs.com/mengdd/p/4191941.html)介绍了AJAX的概念和基本使用,附有一个小例子,下面 ...

  5. 详细解读XMLHttpRequest(一)同步请求和异步请求

    本文主要参考:MDN XMLHttpRequest 让发送一个HTTP请求变得非常容易.你只需要简单的创建一个请求对象实例,打开一个URL,然后发送这个请求.当传输完毕后,结果的HTTP状态以及返回的 ...

  6. OKHttp源码学习同步请求和异步请求(二)

    OKHttp get private void doGet(String method, String s) throws IOException { String url = urlAddress ...

  7. 同步请求和异步请求的区别,ajax异步请求如何理解

    同步请求和异步请求的区别 先解释一下同步和异步的概念 同步是指:发送方发出数据后,等接收方发回响应以后才发下一个数据包的通讯方式. 异步是指:发送方发出数据后,不等接收方发回响应,接着发送下个数据包的 ...

  8. 从零开始学 Web 之 Ajax(五)同步异步请求,数据格式

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  9. 第106天:Ajax中同步请求和异步请求

    同步请求和异步请求的区别 1.同步是指:发送方发出数据后,等接收方发回响应以后才发下一个数据包的通讯方式. 用户填写所有信息后,提交给服务器,等待服务器的回应(检验数据),是一次性的.信息错误又要重新 ...

随机推荐

  1. 前端使用AngularJS的$resource,后端ASP.NET Web API,实现分页、过滤

    在上一篇中实现了增删改查,本篇实现分页和过滤. 本系列包括: 1.前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查2.前端使用AngularJS的$re ...

  2. ASIHTTPRequest学习笔记

    1.creating requestsrequest分为同步和异步两种.不同之处在于开始request的函数:[request startSynchronous];[request startAsyn ...

  3. iOS 使用宏 常量 报错 expected expression

    报错的代码: 报错原因:多写了一个分号!

  4. i386、i586、i686、noarch、x86_64

    xxxxxxxxx.rpm   <== RPM的格式,已经经过编译且包装完成的rpm文件. xxxxxx.src.rpm   <== SRPM的格式,包含未编译的源代码信息. 例如rp-p ...

  5. Getting OS version with NDK in C c++获得版本号

    http://stackoverflow.com/questions/19355783/getting-os-version-with-ndk-in-c #include <cutils/pro ...

  6. 在 Spring 4.3.9下升级 Velocity 1.7.x to Velocity 2.0.x 出现的问题

    1: Spring 的  spring-context-support 报错 java.lang.NoClassDefFoundError: org/apache/velocity/runtime/l ...

  7. head first--------------state pattern

    head first----------浅谈状态模式     状态模式:允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类     实现代码如下:      package com.cl ...

  8. Tensorflow 模型持久化saver及加载图结构

    主要内容: 1. 直接保存,加载模型; (可以指定加载,保存的var_list) 2. 加载,保存指定变量的模型 3. slim加载模型使用 4. 加载模型图结构和参数等 tensorflow 恢复部 ...

  9. Convert Sorted List to Binary Search Tree leetcode java

    题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...

  10. AOP AspectJ 字节码 语法 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...