About xmlrpc of objective-c - 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 ?

Related

objective c how create new playlist on SoundCloud?

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

Unable to convert a string to a NSURL

I am trying to turn off WAL in CoreData using this code:
// Code to disable journaling mode
NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *urlString = [applicationDocumentsDirectory stringByAppendingPathComponent: #"saori.sqlite"];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSDictionary *options = #{NSSQLitePragmasOption:#{#"journal_mode":#"DELETE"}};
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] init];
[psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:nil];
This is my NSString that I need to convert to a URL:
/Users/rolfmarsh/Library/Application Support/iPhone Simulator/7.1/Applications/7A614C51-AC51-4F5B-9716-6E3D2F160324/Documents/saori.sqlite
When I use that string in this statement, it generates nil.
NSURL *url = [[NSURL alloc] initWithString:urlString];
I don't see anything wrong with the string. What am I doing wrong?
Read the docs for NSURL. You have a file path and you need to create a file URL from the file path.
NSURL *url = [NSURL fileURLWithPath:urlString];

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";

iphone SDK: How to post data to a url?

This may be a duplicate question but i could not find my answer when searching. So, How do i post data to a url? Heres what i got so far:
NSString *url = #"https://localhost/login.php";
NSURL *urlr = [NSURL URLWithString:url];
NSMutableURLRequest *urlre = [[NSMutableURLRequest alloc] init];
[urlre setURL:[NSURL URLWithString:url]];
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
NSString *user = [defs stringForKey:#"User"];
NSString *pass = [defs stringForKey:#"Pass"];
NSInteger *version = [defs integerForKey:#"Version"];
NSString *bodyData = [[NSString alloc] initWithFormat:#"user=%#&password=%#&version=%d",user,pass,version];
NSData *body = [bodyData dataUsingEncoding:NSASCIIStringEncoding];
NSURLResponse *response = nil;
NSError *error = nil;
[urlre setHTTPMethod:#"POST"];
[urlre setValue:[[NSString alloc] initWithFormat:#"%d",[body length]] forHTTPHeaderField:#"Content-Length"];
[urlre setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[urlre setHTTPBody:body];
NSData *dataThis = [NSURLConnection sendSynchronousRequest:urlre returningResponse:&response error:&error];
if(dataThis)
{
NSLog(#"Connect Success");
} else {
NSLog(#"%#",[error localizedDescription]);
}
Is the above correct? In my
-(void) connection:(NSURLConnection*)connection didReceiveData:(NSData *)data
event, I get nothing. Even in the didFinishLoading it gets nothing with NSLog. Please help.
Have you set a delegate for your request?
Why don't you have a look at using a framework like ASIHTTPRequest, makes things so simple. Check it out http://allseeing-i.com/ASIHTTPRequest/

NSData to NSString with åöä

I'm downloading a webpage using NSMutableURLRequest but having problem putting that very webpage into a NSString.
NSString *username = #"my_username";
NSString *password = #"my_password";
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:#"https://www.mypage.com/login.php?username=%#&password=%#", username, password]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnPage = [NSString stringWithFormat:#"%.*s", [returnData length], [returnData bytes]];
This works fine except for special chars like åäö etc. Is there any better way?
Yes, use the following:
NSString *returnPage = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];
That uses UTF8 instead of ASCII.