iOS Web Service似乎没有启动。无论如何,我可以找出它做了什么吗?

时间:2014-03-07 13:16:10

标签: ios web-services

我已经构建了一个WebService来使用PHP API检索用户特定的报告。我对此很新,所以我按照一些教程和帮助页面来构建Web服务。它似乎根本没有运行所以我认为我填满或者只是错误地将代码放入不属于我不同的代码中,因为我正在遵循多个教程等以获得所需的结果。

这是Web服务的.m文件的代码:

#import "reportsTestViewController.h"
#import "ReportsDataObject.h"

@interface reportsTestViewController ()

@end

@implementation reportsTestViewController

@synthesize label;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

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

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.tesg.com.au/allCustBuild.php"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (connection) {
        //connect
        label.text = @"connecting...";
    } else {
        //error
    }

}

-(void)setupReportsFromJSONArray:(NSData*)dataFromReportsArray{
    NSError *error;
    NSMutableArray *reportsArray = [[NSMutableArray alloc] init];
    NSArray *arrayFromServer = [NSJSONSerialization JSONObjectWithData:dataFromReportsArray options:0 error:&error];

    if(error){
        NSLog(@"error parsing the json data from server with error description - %@", [error localizedDescription]);
    }
    else {
        reportsArray = [[NSMutableArray alloc] init];
        for(NSDictionary *eachReport in arrayFromServer)
        {
            ReportsDataObject *report = [[ReportsDataObject alloc] initWithJSONData:eachReport];
            [reportsArray addObject:report];
        }

        //Now you have your reportsArray filled up with all your data objects
    }
}

-(void)connectionWasASuccess:(NSData *)data{
    [self setupReportsFromJSONArray:data];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //We check against table to make sure we are displaying the right number of cells
    // for the appropriate table. This is so that things will work even if one day you
    //decide that you want to have two tables instead of one.
    if(tableView == reportsTable){
        return([theReportsArray count]);
    }
    return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell)
    {
        //set your configuration of your cell
    }
    //The beauty of this is that you have all your data in one object and grab WHATEVER you like
    //This way in the future you can add another field without doing much.

    if([theReportsArray count] == 0){
        cell.textLabel.text = @"no reports to show";
    }
    else{
        ReportsDataObject *currentReport = [theReportsArray objectAtIndex:indexPath.row];
        cell.textLabel.text = [currentReport buildingName];
        // in the future you can grab whatever data you need like this
        //[currentPlace placeName], or [currentPlace placeDay];
    }
    return(cell);
}

@end

和.h的代码:

#import <UIKit/UIKit.h>

@interface reportsTestViewController : UIViewController  <UITableViewDelegate, UITableViewDataSource>{

    IBOutlet UITableView *reportsTable;
    IBOutlet UILabel *Label;

    NSArray *theReportsArray;
}

@property (nonatomic, retain) IBOutlet UILabel *label;

@end

我确信这是一件非常无知​​的事情,但是在通过帮助页面和tuts之后,我找不到我做错了什么。

2 个答案:

答案 0 :(得分:1)

看起来您正在建立连接,但您永远不会处理任何被发回的数据。创建连接后,需要添加委托方法来处理连接发回的数据。

正如一些评论中所提到的,您还需要验证服务器上的php页面确实为您提供了所需的信息。通过在-connectionDidFinishLoading中注销数据字符串,您将能够看到从服务器发回的任何数据以进行调试。

//create an NSMutableData property in your interface
@property (nonatomic, strong) NSMutableData *myDataIvar;

//initialize it when you create your connection
if (connection){
    self.myDataIvar = [[NSMutableData alloc] init];
}


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

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

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"Connection Failed: %@", error.userInfo);
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    //this is where you would parse the data received back from the server
    NSString *responseString = [[NSString alloc] initWithData:self.myDataIvar encoding:NSUTF8StringEncoding];
    NSLog(@"Received Data: %@",responseString);
    [self setupReportsFromJSONArray:self.myDataIvar];
}

这样至少可以看到您从服务器接收的数据。

编辑: 此外,您的测试页面似乎没有输出任何json数据。当我导航到它时,我得到的只是“如何”。 我建立了一个快速的php页面,你可以用来测试你的obj-c代码。如果您的代码正确,它将回显一个包含10个测试结果的数组,以填充您的tableview。

//Create your request pointing to the test page
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.codeyouniversity.com/json_test.php"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];

如果您想将php放在您自己的页面上进行测试,这就是我使用的。

<?php
    $jsonArray = array('result1','result2','result3','result4','result5','result6','result7','result8','result9','result10');

    echo json_encode($jsonArray);
?>

这里也是URL加载系统的Ap​​ples文档的链接 https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html#//apple_ref/doc/uid/10000165i

答案 1 :(得分:0)

如果你得到结果就试试这个,你的网址是错的。我想现在你自己的网址是错误的

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.openweathermap.org/data/2.5/weather?q=London,uk"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (connection) {
        //connect
        label.text = @"connecting...";
    } else {
        //error
    }

}