0

I am working on a web service app and i have a question. I am calling a specific Url site with the following code:

NSURL *Url = [NSURL URLWithString:[requestStream stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSString *htmlString = [NSString stringWithContentsOfURL:Url encoding:NSUTF8StringEncoding error:&error];

Then i create an nsstring and print it with the NSLog and i got the following results:

2014-05-14 15:50:58.118 jsonTest[1541:907] Data : Array
(
    [url] => http://sheetmusicdb.net:8000/hnInKleinenGruppen
    [encoding] => MP3
    [callsign] => SheetMusicDB.net - J
    [websiteurl] => 
)

My question in how can i parse the URL link and then use it as a value? I tried to put them to an array or a dictionary but i get error back. So if anyone can help me i would appreciate it.

Ok i managed to grab the link with the following code:

if(htmlString) {
    //NSLog(@"HTML %@", htmlString);

    NSRange r = [htmlString rangeOfString:@"=>"];
    if (r.location != NSNotFound) {
        NSRange r1 = [htmlString rangeOfString:@"[encoding]"];
        if (r1.location != NSNotFound) {
            if (r1.location > r.location) {
                _streamTitle = [htmlString substringWithRange:NSMakeRange(NSMaxRange(r), r1.location - NSMaxRange(r))];
                NSLog(@"Title %@", _streamTitle);
            }
        }
    }
} else {
    NSLog(@"Error %@", error);
}

Thank you for your suggestions. My problem now is that i pass the string into an avplayer and i can't hear music.

5
  • its not proper response Commented May 14, 2014 at 13:04
  • What exactly do you mean Kirit? Commented May 14, 2014 at 13:05
  • use JSON parsing, its better for you. Commented May 14, 2014 at 13:07
  • Can you give me your URL Commented May 14, 2014 at 13:07
  • Yes this is the URL with JSON request:dar.fm/… Commented May 14, 2014 at 13:13

2 Answers 2

3

I think this will be help for you..

// @property (nonatomic,retain) NSMutableData *responseData;
// @synthesize responseData;

NSString *urlString=@"http:your urls";
self.responseData=[NSMutableData data];
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] delegate:self];
NSLog(@"connection====%@",connection);

JSON Delegates :

 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 {
   [self.responseData setLength:0];
 }
 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 {
    [self.responseData appendData:data];
 }
 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
 {
    self.responseData=nil;
 }
 -(void)connectionDidFinishLoading:(NSURLConnection *)connection
 {
      NSArray *response_Array = [NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:nil];

  //  Write code for retrieve data according to your Key.
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Eesdee i tried your example and after NSArray i wrote the following code with no results. It returns NULL. NSDictionary* arrayDic = [response_Array objectAtIndex:0];NSString * arrayStr = [arrayDic objectForKey:@"Array"]; NSLog(@"JSON url = %@", arrayStr);
eesdee here is a valid url after a search of my implementation gr-relay-12.gaduradio.pl/25
1

Try This:

NSString *urlStr=[NSString stringWithFormat:@"uRL"];
 NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

        if (theConnection)
        {
            receivedData=[[NSMutableData data] retain] ;
        }


#pragma mark - Connection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    // append the new data to the receivedData
    // receivedData is declared as a method instance elsewhere

    //NSLog(@"data %@",data);
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    // release the connection, and the data object
    //    [connection release];
    //    [receivedData release];

    UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Connection Failed" message:[error localizedDescription]delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [prompt show];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {


    NSString *content = [[NSString alloc]  initWithBytes:[receivedData bytes]
                                                  length:[receivedData length] encoding: NSUTF8StringEncoding];
    NSData *jsonData = [content dataUsingEncoding:NSUTF32BigEndianStringEncoding];

    NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];


    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.