iOS.访问 Web Service.异步POST请求方法

#import <UIKit/UIKit.h>
#import "T20140628024917NSNumber+Message.h"
#import "T20140628024917NSString+URLEncoding.h"

@interface T20140628024917ViewController : UITableViewController<NSURLConnectionDelegate>

@property (nonatomic,strong) NSMutableArray *listData;

//接收从服务器返回数据。
@property (strong,nonatomic) NSMutableData *datas;

// 查询所有
-(void)findAll;

@end
#import "T20140628024917ViewController.h"

@interface T20140628024917ViewController ()

@end

@implementation T20140628024917ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 1、初始化数据
    [self findAll];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark table dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1、初始化重用Cell
    static NSString *reUseCell = @"reUseCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reUseCell];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reUseCell];
    }

    // 2、配置重用Cell数据
    NSMutableDictionary*  dict = self.listData[indexPath.row];
    cell.textLabel.text = [dict objectForKey:@"Content"];
    cell.detailTextLabel.text = [dict objectForKey:@"CDate"];
    return cell;
}
-(void)findAll
{
    NSString *strURL = [[NSString alloc] initWithFormat:@"http://127.0.0.1:8080/kujizu/test01.html"];
    NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]];
    NSString *post = [NSString stringWithFormat:@"email=%@&type=%@&action=%@", @"<你的iosbook1.com用户邮箱>",@"JSON",@"query"];
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];

    NSURLConnection *connection = [[NSURLConnection alloc]
                                   initWithRequest:request delegate:self];

    if (connection) {
        _datas = [NSMutableData new];
    }
}
#pragma mark- NSURLConnection 回调方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_datas appendData:data];
}

-(void) connection:(NSURLConnection *)connection didFailWithError: (NSError *)error {

    NSLog(@"%@",[error localizedDescription]);
}

- (void) connectionDidFinishLoading: (NSURLConnection*) connection {
    NSLog(@"请求完成...");
    NSDictionary* resDict = [NSJSONSerialization JSONObjectWithData:_datas options:NSJSONReadingAllowFragments error:nil];

    if (resDict == nil) {

        self.listData = [[NSMutableArray alloc] init];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"错误信息" message:@"没有数据。" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alertView show];
    }else{

        NSNumber *resultCodeObj = [resDict objectForKey:@"ResultCode"];
        if ([resultCodeObj integerValue] >=0){

            self.listData = [resDict objectForKey:@"Record"];
            [self.tableView reloadData];
        } else {

            NSString *errorStr = [resultCodeObj errorMessage];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"错误信息" message:errorStr delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alertView show];
        }
    }

}
@end

iOS.访问 Web Service.异步POST请求方法

时间: 2024-11-10 08:15:26

iOS.访问 Web Service.异步POST请求方法的相关文章

iOS.访问 Web Service.异步GET请求方法

#import <UIKit/UIKit.h> #import "T20140628024750NSNumber+Message.h" #import "T20140628024750NSString+URLEncoding.h" @interface T20140628024750ViewController : UITableViewController<NSURLConnectionDelegate> @property (nonato

iOS.访问 Web Service.同步GET请求方法

1.字符串转换为URL字符串NSString分类 #import <Foundation/Foundation.h> @interface NSString (URLEncoding) -(NSString *)URLEncodedString; -(NSString *)URLDecodedString; @end #import "T20140628013418NSString+URLEncoding.h" @implementation NSString (URLEn

iOS.访问 Web Service.MKNetworkKit_POST

#import <UIKit/UIKit.h> #import "T20140628025249NSNumber+Message.h" #import "T20140628025249NSString+URLEncoding.h" #import "MKNetworkEngine.h" #import "MKNetworkOperation.h" @interface T20140628025249ViewCont

iOS.访问 Web Service.MKNetworkKit_GET

#import <UIKit/UIKit.h> #import "T20140628025200NSNumber+Message.h" #import "T20140628025200NSString+URLEncoding.h" #import "MKNetworkEngine.h" #import "MKNetworkOperation.h" @interface T20140628025200ViewCont

iOS.访问 Web Service.使用下拉刷新控件

#import <UIKit/UIKit.h> #import "T20140628025702NSNumber+Message.h" #import "T20140628025702NSString+URLEncoding.h" @interface T20140628025702ViewController : UITableViewController @property (nonatomic,strong) NSMutableArray *lis

iOS 开发指南 第15章 访问Web Service之REST Web Service

***** 在电脑术语中,统一资源标识符(Uniform Resource Identifier,或URI)是一个用于标识某一互联网资源名称的字符串. 该种标识允许用户对任何(包括本地和互联网)的资源通过特定的协议进行交互操作.URI由包括确定语法和相关协议的方案所定义. Web上可用的每种资源 -HTML文档.图像.视频片段.程序等 - 由一个通用资源标识符(Uniform Resource Identifier, 简称"URI")进行定位. ***** 1 REST Web Ser

iphone ios 用xcode4.2开发 访问web service的功能

http://blog.csdn.net/panyaorui/article/details/8622990 1.后台利用 cxf 构建一个web service服务. HelloWorld.java [java] view plaincopy /** * */ package com.alcor.jws.test; import javax.jws.WebMethod; import javax.jws.WebService; import org.apache.cxf.feature.Fea

SAP Web Service简介与配置方法

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h1 { margin-top: 17.0000pt; margin-bottom: 16.5000pt; margin-left: 0.0000pt; text-indent: 21.0000pt; page-break-after: avoid; text-al

Java通过Axis访问Web Service

在使用Axis访问Web Service时,需要引入以下包(10个):axis-ant.jar.axis.jar.commons-discovery-0.2.jar.commons-logging-1.0.4.jar.jaxrpc.jar.log4j-1.2.8.jar.saaj.jar.wsdl4j-1.5.1.jar.activation-1.1.jar和mail-1.4.jar. 下面是一段Java代码的例子: 1 package demo; 2 import javax.xml.name