0

I need to get the text from URL http://cast.midiaip.com.br:6530/currentsong?sid=1 and display it in a UILabel My code is like that: ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *musicasLabel;
- (IBAction)textoButton:(id)sender;
@property (strong, nonatomic) IBOutlet UIWebView *webView;

@end

ViewController.m

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize musicasLabel;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)textoButton:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://cast.midiaip.com.br:6530/currentsong?sid=1"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *string = [NSString stringWithUTF8String:[data bytes]];
musicasLabel.text = string;
}
@end

I always get the error:

2015-04-17 02:55:21.637 teste label[14194:797672] *** 
Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: '***
 +[NSString stringWithUTF8String:]: NULL cString'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010cf1bf35
__exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010cbb4bb7 objc_exception_throw + 45
2   CoreFoundation                      0x000000010cf1be6d +[NSException raise:format:] + 205
3   Foundation                          0x000000010c719464 +[NSString stringWithUTF8String:] + 78
4   teste label                         0x000000010c6885e5 -[ViewController textoButton:] + 181
5   UIKit                               0x000000010d3098be -[UIApplication sendAction:to:from:forEvent:] + 75
6   UIKit                               0x000000010d410410 -[UIControl _sendActionsForEvents:withEvent:] + 467
7   UIKit                               0x000000010d40f7df -[UIControl touchesEnded:withEvent:] + 522
8   UIKit                               0x000000010d34f308 -[UIWindow _sendTouchesForEvent:] + 735
9   UIKit                               0x000000010d34fc33 -[UIWindow sendEvent:] + 683
10  UIKit                               0x000000010d31c9b1 -[UIApplication sendEvent:] + 246
11  UIKit                               0x000000010d329a7d _UIApplicationHandleEventFromQueueEvent + 17370
12  UIKit                               0x000000010d305103 _UIApplicationHandleEventQueue + 1961
13  CoreFoundation                      0x000000010ce51551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14  CoreFoundation                      0x000000010ce4741d __CFRunLoopDoSources0 + 269
15  CoreFoundation                      0x000000010ce46a54 __CFRunLoopRun + 868
16  CoreFoundation                      0x000000010ce46486 CFRunLoopRunSpecific + 470
17  GraphicsServices                    0x00000001104ea9f0 GSEventRunModal + 161
18  UIKit                               0x000000010d308420 UIApplicationMain + 1282
19  teste label                         0x000000010c688ae3 main + 115
20  libdyld.dylib                       0x000000010f4ab145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Thank you very much!!!

1 Answers1

0

You are initializing your string incorrectly. Your question is answered here: convert UTF-8 encoded NSData to a NSString

Community
  • 1
  • 1
  • thank you for the answer, but this did't work, the label is blank. my code: - (IBAction)textoButton:(id)sender { NSURL *url = [NSURL URLWithString:@"http://cast.midiaip.com.br:6530/currentsong?sid=1"]; NSData *data = [NSData dataWithContentsOfURL:url]; NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; musicasLabel.text = string; NSLog(@" Data %@, string %@", data, string); the log: 2015-04-17 04:27:54.091 teste label[15141:855436] Data (null), string – Fernando Costa Neto Apr 17 '15 at 07:29
  • This code: `NSURL *url = [NSURL URLWithString:@"http://cast.midiaip.com.br:6530/currentsong?sid=1"]; NSData *data = [NSData dataWithContentsOfURL:url]; NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];` worked for me. I got the correct `NSString` value. – Larme Apr 17 '15 at 07:44
  • didn't work for me, my label is blank, and i use nslog and the data is null and the string blanc. What i did wrong? thanks – Fernando Costa Neto Apr 17 '15 at 23:55