簡體   English   中英

iOS WebService似乎尚未啟動。 無論如何,我可以找出它是否在做什么?

[英]iOS WebService doesn't seem to be starting. Anyway I can find out if its doing anything?

我已經構建了一個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

我敢肯定我所做的事情確實很無知,但是在返回幫助頁面和教程之后,我找不到我做錯了什么。

看起來您正在建立連接正常,但是您永遠不會處理任何發送回的數據。 創建連接后,需要添加委托方法來處理由連接發送回的數據。

如某些評論中所述,您還需要驗證服務器上的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個測試結果的數組以填充表格視圖。

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

如果得到結果,請嘗試這個,您的網址是錯誤的。我認為現在您的網址本身是錯誤的

- (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
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM