iOS開發——網絡編程

//
//  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

發佈了69 篇原創文章 · 獲贊 8 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章