objective c how create new playlist on SoundCloud? - objective-c

Trying to create new playlist, but it doesn't work.
Function for request:
-(NSURLRequest *)makeNewPlaylistsRequestNamed:(NSString *)name
{
NSURL *url = [NSURL URLWithString:#"https://api.soundcloud.com/playlists"];
NSURLComponents *comp = [NSURLComponents componentsWithURL:url
resolvingAgainstBaseURL:YES];
NSMutableArray *qi = [[NSMutableArray alloc] init];
[qi addObject:[NSURLQueryItem queryItemWithName:#"oauth_token" value:self.authResult.value]];
comp.queryItems = qi;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:comp.URL];
request.HTTPMethod = #"PUT";
NSDictionary *newPL = #{#"title":name,#"sharing":#"public",#"tracks":#[]};
NSDictionary *gistDict = #{#"playlist":newPL};
NSError *jsonError;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:gistDict
options:NSJSONWritingPrettyPrinted
error:&jsonError];
[request setHTTPBody:jsonData];
return request;
}
But new playlist doesn't creates.
I used web API - https://developers.soundcloud.com/docs/api/reference#playlists

Related

Trouble Storing URL in NSURL Variable

At the end of this method, "address" (NSURL variable) returns nil, even though "countyName" returns #"Alameda". What am I doing wrong?
-(void) getDataUrl {
if ([countyName isEqual: #"Alameda"]) {
NSURL * fileURL = [[NSURL alloc] initWithString:#"http://www.myservername.com/alameda.php"];
self.address = fileURL;
}
if ([countyName isEqual: #"Napa"]) {
NSURL * fileURL = [[NSURL alloc] initWithString:#"http://www.myservername.com/napa.php"];
self.address = fileURL;
}
if ([countyName isEqual: #"San Luis Obispo"]) {
NSURL * fileURL = [[NSURL alloc] initWithString:#"http://www.myservername.com/sanluisobispo.php"];
self.address = fileURL;
}
}
The solution here was to create a plist containing an array of dictionaries. Each dictionary held the name of a county and a url for the php script for that county. The plist was used to populate a tableview, and then the selected row was pushed to another tableview using the variable "countyName". "countyName" held the county name and the url. I then used countyName in my retrieveData method like this:
NSURL *url = [NSURL URLWithString:[countyName valueForKey:#"weburl"]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
Thanks to everyone for taking the time to respond.

REST API with JSON in iPhone

I want to parse REST API with JSON is there any Best Example or Tutorial so help me...... Like I want to post request like Login and Register and then parse Feed using REST API..... I am using below code to post request
NSString *email = #"*******";
NSString *password = #"******";
NSString *apikey = #"********";
NSString *loginURL = #"***************";
NSURL *url = [NSURL URLWithString:loginURL];
NSString *JSONString = [NSString stringWithFormat:#"{\"apikey\":\"%#\",\"email\":\"%#\",\"password\":\"%#\"}", apikey, email, password];
NSData *JSONBody = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *loginRequest = [[NSMutableURLRequest alloc] initWithURL:url];
loginRequest.HTTPMethod = #"POST";
loginRequest.HTTPBody = JSONBody;
NSOperationQueue *queue = [NSOperationQueue new];
[NSURLConnection sendAsynchronousRequest:loginRequest
queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
NSLog(#"resp:%#",response);
// Manage the response here.
}];
To parse the JSON response, you will need to implement
id obj = [NSJSONSerialization JSONObjectWithData:data];
in your completionHandler.
To get NSDictionary object from server response you may use :
NSDictionary *myDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error: &error];
After that you can get objects and keys from myDict
The good tutorial you can find here http://www.raywenderlich.com/5492/
Try this
NSDictionary *sampledict = #{#"email":email,#"password":password,#"apikey":apikey};
NSData *dataJson = [NSJSONSerialization dataWithJSONObject:sampledict options:0 error:NULL];
NSMutableURLRequest *loginRequest = [[NSMutableURLRequest alloc] initWithURL:url];
loginRequest.HTTPMethod = #"POST";
loginRequest.HTTPBody = dataJson;
//add these lines
[loginRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[loginRequest setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
//remaining is the same in your code

About xmlrpc of objective-c

Recently I have been working about an iOS project.I want to send my picture to an server.I use xmlrpc to do this ,but when I write codes like this:
- (void)uploadImage:(UIImage *)sourceImage ForName:(NSString *)imageName
{
NSData *imageData = UIImagePNGRepresentation(sourceImage);
NSString *imageString = [imageData base64EncodedString];
NSURL *URL = [NSURL URLWithString: SERVER_URL];
XMLRPCRequest *request = [[XMLRPCRequest alloc]
initWithURL:URL];
NSArray *params = [NSArray arrayWithObjects:imageName,imageString,nil];
[request setMethod:#"upload_image" withParameter:params];
I give "imageName" to params,it will fail.But if I write code like this
- (void)uploadImage:(UIImage *)sourceImage
{
NSData *imageData = UIImagePNGRepresentation(sourceImage);
NSString *imageString = [imageData base64EncodedString];
NSURL *URL = [NSURL URLWithString: SERVER_URL];
XMLRPCRequest *request = [[XMLRPCRequest alloc]
initWithURL:URL];
NSString *imageName = #"hello.png";
NSArray *params = [NSArray arrayWithObjects:imageName,imageString,nil];
[request setMethod:#"upload_image" withParameter:params];
It will be ok and I can upload the picture to the server.
I don't know why,can someone explain it ?

NSURLConnection (Syncronous Request) returns (null)

I want to get HTML from website. I wrote below code but this returns (null).
NSString *strURL = #"http://www.googole.com";
- (NSString *)getHtml
{
NSURL *url = [NSURL URLWithString: strURL];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest : req
returningResponse : &response
error : &error];
NSString *strData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return strData;
}
I think you have mistyped the url.
It should be NSString *strURL = #"http://www.google.com";

EXC_BAD_ACCESS error occured when using stringWithFormat

this is my header section:
#interface RootViewController : UIViewController
{
NSString *status_id;
}
in the controller file, i am assigning the variable:
- (void)updateStatus
{
NSURL *url = [NSURL URLWithString:#"http://localhost/RightNow/API/status.json"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
NSString *response = [NSString alloc];
NSError *error2;
NSData* data = [response dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error2];
status_id = [json objectForKey:#"id"];
}
now, when i try to use the status_id again, i get the error
- (IBAction)likeClick:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://localhost/RightNow/API/vote"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request setPostValue:status_id forKey:#"id"]; //The error comes here
[request setPostValue:#"like" forKey:#"vote"];
[request startSynchronous];
}
sorry about my bad english.
please help me, thank you!
[json objectForKey:#"id"]; will return the object in autorelease pool. You either need to send a copy message to it like
status_id = [[json objectForKey:#"id"] copy];
and release it when appropriate (if not using ARC)