iOS基本请求到webservice

时间:2013-01-14 09:35:13

标签: ios json http request

任何人都可以指出我正确的教程方向或可能回答这个问题。

我有一些JSON要在我的网络服务器上返回

{
first_name: "Joe";
last_name: "Smith";
department: "Human Resources";
}

如何通过点击按钮发出http请求获取此信息并在iphone上显示为文本。?

完成新手所以请'哑巴'。

2 个答案:

答案 0 :(得分:2)

创建一个jsonviewcontroller类

#import <UIKit/UIKit.h>

@interface JSONViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,NSURLConnectionDataDelegate>
{
    NSString *label;
}
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
- (IBAction)getTop10AlbumAction:(id)sender;


@end

然后在实现类中: -

#import "JSONViewController.h"

@interface JSONViewController ()
{
    NSMutableData *webData;
    NSURLConnection *connection;
    NSMutableArray *array;
    NSString *category;    
}

@end

@implementation JSONViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    array=[[NSMutableArray alloc]init];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Failed with Error");
}

这是实际解析发生的地方,我们从相应的字典和数组中获取相册的标题。首先要了解这个链接http://jsonviewer.stack.hu/#http://itunes.apple.com/us/rss/topalbums/limit=10/json这是json查看器,它显示了我们要访问的内容的结构。不要惊慌!如果你尝试解析一些json网址,它真的很容易

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSDictionary *allDataDictionary=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
    NSDictionary *feed=[allDataDictionary objectForKey:@"feed"];
    NSArray *arrayOfEntry=[feed objectForKey:@"entry"];
    for (NSDictionary *dict in arrayOfEntry) {
        NSDictionary *title=[dict objectForKey:@"title"];
        NSString *label=[title objectForKey:@"label"];
        [array addObject:label];
    }

[[self myTableView]reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return[array count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    //clear background color
    [[cell textLabel] setBackgroundColor:[UIColor clearColor]];
    [[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];

    //set cell text
    cell.textLabel.text=[array objectAtIndex:indexPath.row];

    //set cell accessory type
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

这里我们指定要解析的itunes url并发出连接请求

- (IBAction)getTop10AlbumAction:(id)sender {
    NSURL *url=[NSURL URLWithString:@"http://itunes.apple.com/us/rss/topalbums/limit=10/json"];    
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    connection=[NSURLConnection connectionWithRequest:request delegate:self];
    if (connection) {
        webData=[[NSMutableData alloc]init];
    }
    [[self myTableView]reloadData];
}
@end

我希望我已经说清楚了!

答案 1 :(得分:1)

我建议您使用两种不同的教程:

  1. 创建一个简单的界面。以下是Apple的示例,其中显示了一个按钮和一个文本:iOS Button - Label sample

  2. 将调用网络服务的代码插入控制器代码(即changeGreeting:):iOS JSON Webservice tutorial