//
// ViewController.m
// 网络编程
//
// Created by Quanzheng on 14-9-10.
// Copyright (c) 2014年 QuanZHeng. All rights reserved.
//
#import "ViewController.h"
#define BASE_URL @"http://project.lanou3g.com/teacher/yihuiyun/phpJSON.php"
#define BASE_URL_2 @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"
#define BASE_URL_DATA @"date=20131129&startRecord=5&len=5&udid=1234567890&terminalType=Iphone&cid=215"
@interface ViewController ()
@property (retain, nonatomic) IBOutlet UITextView *textvvew;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark get同步请求
- (IBAction)getTongbuButtonAction:(UIButton *)sender {
// 1/准备url地址
NSString *urlString = BASE_URL;
NSURL *url = [NSURL URLWithString:urlString];
// 2、创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 使用url创建出对象
// 2.1请求方式(默认方式为GET,可不写)
[request setHTTPMethod:@"GET"];
// 3.创建请求返回的对象
NSURLResponse *response = nil;
NSError *error = nil;
// 4.创建链接对象,发送请求(同步)
NSData *resultData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
NSLog(@"%@",resultData);
//5.解析data类型数据,进行显示
NSArray *array = [NSJSONSerialization JSONObjectWithData:resultData
options:NSJSONReadingAllowFragments
error:nil];
NSLog(@"array = %@",array);
// 调用自己写的显示数据的方法来在textview上显示数据
[self showResultInfo:array];
}
#pragma mark post同步请求
- (IBAction)postTongbuButtonAction:(UIButton *)sender {
// 1.设置地址
NSURL *url = [NSURL URLWithString:BASE_URL_2];
// 2.设置请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 2.1 设置请求方式
[request setHTTPMethod:@"POST"];
// 2.2设置请求参数
NSString *param = BASE_URL_DATA;
NSData *paramData = [param dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:paramData];
// 3.创建连接对象,发送请求
NSData *resultData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// 4.解析结果,并显示
NSArray *array = [NSJSONSerialization JSONObjectWithData:resultData options:NSJSONReadingAllowFragments error:nil];
[self showResultInfo:array];
}
- (IBAction)getYibuButtonAction:(UIButton *)sender {
}
- (IBAction)postYibuButtonAction:(UIButton *)sender {
}
- (void)dealloc {
[_textvvew release];
[super dealloc];
}
#pragma mark - 显示数据
- (void)showResultInfo:(id)data
{
_textvvew.text = [NSString stringWithFormat:@"%@",data];
}
@end
iOS开发——网络编程
最新推荐文章于 2022-10-21 19:23:54 发布