简体   繁体   中英

How to convert NSdata to string in ios

I'm trying to view the video in tableview from my server so fetching the video from the server using the json in ios. I have successfully connected the url to nsurl connection and appending to the nsdata everything is fine but the problem is when converting the nsdata to string it through the null.

this is code I have used for the url connection:

-(void)setDataSource:(vedios *)inVideosObj
{
    //here I'm connecting the nsobject to outlets of the tablecell so here have successfully connected to url connection video is nsobject

    self.titile.text = inVideosObj.title;

    url =[NSURL URLWithString:inVideosObj.video];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];

    NSLog(@"UDSA %@  %@ ",url,request);

    connection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.responseData = [[NSMutableData alloc]init];
}

after that I have checked that rul request passing in console I'm getting the url like this:

2014-01-25 13:00:35.587 video[1835:a0b] UDSA http://localhost/image/vedios/reddy.mp4  <NSURLRequest: 0xb992c40> { URL: http://localhost/image/vedios/reddy.mp4 } 

after I'm appending to the nsdata this is the code:

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

     //NSLog(@"dad %@",self.responseData);
}

once again I printed the nsdata which I have append to in my console im getting like this:

  2014-01-25 14:07:42.024 video[1911:a0b] dad <0000001c 66747970 6d703432 00000000    69736f6d 61766331 6d703432 00008d64 6d6f6f76 0000006c 6d766864 00000000 c8a43177 c8a43177 00000258 00010b78 00010000 01000000 00000000 00000000 00010000 00000000 00000000 00000000 00010000 00000000 00000000 00000000 40000000 00000000 00000000 00000000 00000000 00000000 00000000 00000003 00000015 696f6473 00000000 1007004f ffff2915 ff000052 19747261 6b000000 5c746b68 64000000 01c8a431 77c8a431 77000000 01000000 0000010b 6d000000 00000000 00000000 00010000 00000100 00000000 00000000 00000000 

now here I'm trying to convert the nsdata to string and passing the movieplayer

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *urlString = [[NSString alloc]initWithData:self.responseData encoding:NSASCIIStringEncoding];

    // NSString *urlString = [NSString stringWithUTF8String:[self.responseData bytes] ];

    NSLog(@"data :%@",urlString);
    MPMoviePlayerController *mov = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:urlString]];
//  MPMoviePlayerController *mov = [[MPMoviePlayerController   alloc]initWithContentURL:[NSURL URLWithString:urlString]];
    NSLog(@"length %d",[self.responseData length]);

    // MPMoviePlayerController *mov = [[MPMoviePlayerController   alloc]initWithContentURL:url];

    self.movieplayer = mov;
    [self.movieplayer.view setFrame:CGRectMake(10,20, 100, 100)];

    // self.movieplayer.movieSourceType = MPMovieSourceTypeFile;
    [self.contentView addSubview:self.movieplayer.view];
    [self.movieplayer prepareToPlay];
    [self.movieplayer play];
}

after converting video not playing the tablevewcell I now printed the converted the nsdata sring it's giving the null in my console

2014-01-25 14:12:18.622 video[1934:a0b] data :

I don't no what to do im stuck here for very long I have everything to convert the nsdata to string its showing the same null

this are the link I have tied for the solution research links1 link2

nothing is working please help me how to resolve this problem where I'm doing wrong

this is the output for the self.responsedata

014-01-25 14:24:08.151 video[2019:a0b] reponse=<0000001c 66747970 6d703432 00000000 69736f6d 61766331 6d703432 00008d64 6d6f6f76 0000006c 6d766864 00000000 c8a43177 c8a43177 00000258 00010b78 00010000 01000000 00000000 00000000 00010000 00000000 00000000 00000000 00010000 00000000 00000000 00000000 40000000 00000000 00000000 00000000 00000000 00000000 00000000 00000003 00000015 696f6473 00000000 1007004f ffff2915 ff000052 19747261 6b000000 5c746b68 64000000 01c8a431 77c8a431 77000000 01000000 0000010b 6d000000 00000000 00000000 00010000 00000100 00000000 00000000 00000000 00000100 00000000 00000000 00000000 00400000 00000000 

It is very strange thing you do here

   NSString *urlString = [[NSString alloc]initWithData:self.responseData encoding:NSASCIIStringEncoding];

   MPMoviePlayerController *mov = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:urlString]];

In your self.responseData you've got NSData of required mp4 file. You only need to store it

[self.responseData writeToFile:pathToFile atomically:NO];

in Documents directory as it is. Then you should make local NSURL with path of file pathToFile

NSURL *localURL = [NSURL fileURLWithPath:pathToFile];

and use it

MPMoviePlayerController *mov = [[MPMoviePlayerController alloc]initWithContentURL:localURL];

What kind of data is present in inVideosObj.video?

If it is url for the video, the you should pass this url directly in MPMoviePlayerController .

MPMoviePlayerController *mov = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:inVideosObj.video]];

If it contains the video data, then you need to write that data in a file and give file path in MPMoviePlayerController .

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"video.mp4"];

[inVideosObj.video writeToFile:path atomically:YES];

NSURL *videoUrl= [NSURL fileURLWithPath:path];

MPMoviePlayerController *mov = [[MPMoviePlayerController alloc] initWithContentURL: videoUrl];

This this, if your data is not null terminated,

NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

If your data is null terminated, try this:

NSString *str = [NSString stringWithUTF8String:[data bytes]];

Edit

Check this , it may help. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM