IOS_UI_网络数据请求

本文详细介绍了在iOS开发中如何使用GET和POST方法发送网络请求,并演示了同步与异步请求的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


GET和POST数据请求,同步和异步发送请求

#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;



@end

#import "AppDelegate.h"

#import "MainViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate

- (void)dealloc

{

    [_window release];

    [super dealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    [_window release];

    

    MainViewController *main = [[MainViewController alloc]init];

    self.window.rootViewController = main;

    [main release];

    

    return YES;

}

#import <UIKit/UIKit.h>


@interface MainViewController : UIViewController


@end

#import "MainViewController.h"


@interface MainViewController ()

@property (retain, nonatomic) IBOutlet UIImageView *imageView;

- (IBAction)buttonAction1:(UIButton *)sender;

- (IBAction)buttonAction2:(UIButton *)sender;

- (IBAction)buttonAction3:(UIButton *)sender;

- (IBAction)buttonAction4:(UIButton *)sender;


@end


@implementation MainViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    // 创建请求 发送请求 接收请求

 // 1. 创建请求 GET/POST 

    

    

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


- (IBAction)buttonAction1:(UIButton *)sender {

    

    //1.创建请求 GET

//    NSString *str = @"http://d.hiphotos.baidu.com/image/pic/item/503d269759ee3d6dfe4235d841166d224f4ade19.jpg";

    NSString *str = @"http://img5q.duitang.com/uploads/item/201407/07/20140707201025_JiCF2.jpeg";

//    NSString *str = @"http://img2.imgtn.bdimg.com/it/u=3799452221,1637915779&fm=21&gp=0.jpg";

//    NSString *str = @"http://img1.imgtn.bdimg.com/it/u=3605081051,35197430&fm=21&gp=0.jpg";

//    NSString *str = @"http://img3.imgtn.bdimg.com/it/u=801382299,3525438244&fm=11&gp=0.jpg";

    //对字符串进行编码,将汉字等特殊字符转为UTF8格式

    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:str];

    NSMutableURLRequest  *request = [NSMutableURLRequest requestWithURL:url];

    //出现(NSURL *)类似提示 需要创建一个对象

    //设置网络请求格式

    request.HTTPMethod = @"GET";

    //2. 发送请求 同步/异步

    //(同步)

    NSURLResponse *response = nil;

    NSError *error = nil;

    //参数1: 创建好的请求

    //参数2: 服务器响应信息 取地址

    //参数3: 错误信息

    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    //如果错误存在 打印错误信息

    if (error) {

        NSLog(@"错误信息:%@",error);

    }

    if (response) {

        NSLog(@"服务器响应信息:%@",response);

    }

    

   //3. 处理数据

    UIImage *image = [UIImage imageWithData:data];

    self.imageView.image = image;

    

    

    

    

    

    

}


- (IBAction)buttonAction2:(UIButton *)sender {

    

    //异步请求

    //1. 创建请求

    NSString *str = @"http://img15.3lian.com/2015/f3/08/d/91.jpg";

    //创建一个请求需要一个URL

    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:str];

    NSMutableURLRequest  *request = [NSMutableURLRequest requestWithURL:url];

    //请求方式(GET/POST)

    request.HTTPMethod = @"GET";

    //2. 发送请求(异步连接服务器)

    //参数1: 请求

    //参数2: 请求完成在哪个队列执行代码(UI界面的刷新和视图赋值都要在主队列执行)

    //参数3:

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

       //三个数据response data connectionError

        UIImage *image = [UIImage imageWithData:data];

        self.imageView.image = image;

    }];

    

    

    

    

    

    

    

    

    

}


- (IBAction)buttonAction3:(UIButton *)sender {

    

    //1. 创建请求

    NSString *str = @"http://api.douban.com/v2/movie/nowplaying?app_name=doubanmovie&client=e:iPhone4,1|y:iPhoneOS_6.1|s:mobile|f:doubanmovie_2|v:3.3.1|m:PP_market|udid:aa1b815b8a4d1e961347304e74b9f9593d95e1c5&alt=json&version=2&start=0&city=北京&apikey=0df993c66c0c636e29ecbb5344252a4a";

    //创建一个请求需要一个URL

    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:str];

    NSMutableURLRequest  *request = [NSMutableURLRequest requestWithURL:url];

    //请求方式(GET/POST)

    request.HTTPMethod = @"GET";

    

    //2. 发送请求

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

       //3. 处理数据

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        NSLog(@"%@",dic);

        

        

        

    }];

    

    

    

    

}


- (IBAction)buttonAction4:(UIButton *)sender {

    

    // 1. 创建POST 请求

    NSString *str= @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";

    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:str];

    

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    request.HTTPMethod = @"POST";

    //给POST 请求指定的bodyData

    NSString *bodyStr = @"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";

    //将字符串转为数据类型(NSData)

    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

    //把转换后的数据给网络请求

    request.HTTPBody = bodyData;

    

    //发送请求

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        NSLog(@"%@",dic);

    }];

    

    

    

    

    

    

    

    

    

}

- (void)dealloc {

    [_imageView release];

    [super dealloc];

}

@end

页面用xib拖拽的



资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值