-2

我下面的代码以前可以工作,但突然停止工作。谁能告诉我我做错了什么?

输出NSLog()

2013-05-14 16:06:35.475 Run.PH[1502:19a03] background = (null)
2013-05-14 16:06:35.476 Run.PH[1502:19a03] logo = (null)
2013-05-14 16:06:35.476 Run.PH[1502:19a03] json = (null)
2013-05-14 16:06:35.476 Run.PH[1502:19a03] welcome = (null)

代码:

- (void)viewDidLoad
{

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    NSString *urlString = [NSString stringWithFormat:@"http://run.ph/json/test_json_dyna.jsp"];

    NSURL *url = [NSURL URLWithString:urlString];

    NSData *data = [NSData dataWithContentsOfURL:url];

    if(data != nil)
    {

        NSError *error;

        _json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

        _welcomeMessage.text = [[_json objectForKey:@"welcome"] objectForKey:@"message"];

        NSString *background = [[_json objectForKey:@"welcome"] objectForKey:@"background"];

        NSString *logo = [[_json objectForKey:@"welcome"] objectForKey:@"logo"];


        NSLog(@"background = %@", background);
        NSLog(@"logo = %@", logo);
        NSLog(@"json = %@", _json);
        NSLog(@"welcome = %@", _welcomeMessage.text);


        NSURL *urlBackground = [NSURL URLWithString:background];
        NSData *dataBackground = [NSData dataWithContentsOfURL:urlBackground];

        [_background setImage:[UIImage imageWithData:dataBackground]];

        NSURL *urlLogo = [NSURL URLWithString:logo];
        NSData *dataLogo = [NSData dataWithContentsOfURL:urlLogo];

        [_logo setImage:[UIImage imageWithData:dataLogo]];
    }
    else
    {
        NSLog(@"Error!");
    }


    [self.view reloadInputViews];
}
4

1 回答 1

0

只需 NSLog 您的 URL 响应,您的 json 就会出错。尝试这个

NSString *respo = [NSString stringWithContentsOfURL:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSLog(@"Response - %@",respo);  

它在您的回复中返回“\r”和换行符“\n”。

检查这个

在此处输入图像描述

编辑
现在您的 URL 工作正常,问题出在您的 Web 服务端。

于 2013-05-14T08:38:15.053 回答