使用iOS模拟器进行简单的文本检索

时间:2014-01-01 07:09:38

标签: php ios objective-c httprequest

这是我第一次尝试使用iOS中的PHP脚本检索网络数据。我在www.mobilenicity.com/test.php处有一个简单的PHP脚本,简单地回应了“成功”这个词。我的目的是能够检索此文本并通过NSLog命令输出。

目前,我正在使用iOS模拟器,我不确定这是否会产生我需要处理的任何问题。我可以使用模拟器通过Safari访问网页,所以我知道那里有连接。

我一直盯着这一段时间,但我无法弄清楚我哪里出错了。这就是我所拥有的。 NSLog输出是“下载不起作用”。我知道使用另一个NSLog我在didFailWithError方法中失败了。不幸的是,我不知道如何获得NSNotificationCenter的输出。

有人能看到问题吗?谢谢!

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *s = @"http://wwww.mobilenicity.com/test.php";
    NSURL *url = [NSURL URLWithString:s];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    MyDownloader *d = [[MyDownloader alloc] initWithRequest:req];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(finished:)
                                                 name:@"connectionFinished"
                                               object:d];
    [d.connection start];
}

- (void) finished: (NSNotification *) n
{
    MyDownloader *d = [n object];
    NSData *data = nil;
    if ([n userInfo]) {
        NSLog(@"download didn't work");
    } else {
        data = d.receivedData;
        NSLog(@"%@", data);
    }

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name: @"connectionFinished"
                                                  object:d];
}

MyDownloader.h

#import <Foundation/Foundation.h>

@interface MyDownloader : NSObject

@property (nonatomic, strong, readonly) NSURLConnection *connection;
@property (nonatomic, strong, readonly) NSData *receivedData;

- (id) initWithRequest: (NSURLRequest *) req;
- (void) cancel;
@end

MyDownloader.m

#import "MyDownloader.h"

@interface MyDownloader()

@property (nonatomic, strong, readwrite) NSURLConnection *connection;
@property (nonatomic, copy, readwrite) NSURLRequest *request;
@property (nonatomic, strong, readwrite) NSMutableData *mutableReceivedData;
@end

@implementation MyDownloader

- (NSData *) receivedData
{
    return [self.mutableReceivedData copy];
}

- (id) initWithRequest:(NSURLRequest *)req
{
    self = [super init];

    if (self) {
        self->_request = [req copy];
        self->_connection =
            [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:NO];
        self->_mutableReceivedData = [NSMutableData new];
    }

    return self;
}

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

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

- (void) connection:(NSURLConnection *) connection didFailWithError:(NSError *)error
{
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"connectionFinished"
     object:self userInfo:@{@"error":error}];
}

- (void) connectionDidFinishLoading:(NSURLConnection *) connection
{
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"connectionFinished" object:self];
}

- (void) cancel
{
    // cancel download in progress, replace connection, start over
    [self.connection cancel];
    self->_connection =
        [[NSURLConnection alloc] initWithRequest:self->_request delegate:self startImmediately:NO];
}

@end

3 个答案:

答案 0 :(得分:1)

NSString *text=[[NSString alloc]initWithData:d.receivedData encoding:NSUTF8StringEncoding] ;

答案 1 :(得分:1)

为了调试问题,我在-connection:didFailWithError:中记录了错误,如下所示:

NSLog(@"-connection:didFailWithError: %@", error.localizedDescription);

我收到的错误消息:

A server with the specified hostname could not be found.

这导致我仔细检查您的主机名“http://wwww.mobilenicity.com/test.php”并意识到您有wwww而不是www:)

修复此问题并将下载的数据转换为像Sunny shah所示的字符串,然后你会成为A-OK!

答案 2 :(得分:0)

将viewDidLoad方法更改为以下

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSError *err = nil;
    NSStringEncoding encoding;
    NSString *urlContents = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://wwww.mobilenicity.com/test.php"] usedEncoding:&encoding error:&err];
    if(urlContents)
   {
       NSLog(@"urlContents %@", urlContents);
   } else {
       NSLog(@"err %@",err);
   }
}