I am passing base64 encoded image string to the server side with the HTTP PUT method.I have tried several times but getting error message as response. That error message is because null value is getting in the server side. I have checked the same, in android code and its working fine there. I am attaching my code below.
SBJSON *parser = [[SBJSON alloc] init];
NSString *url=[NSString stringWithFormat:#"The URL i am using"] ;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"XXXXX" forHTTPHeaderField:#"USER_AGENT"];
[request setHTTPMethod:#"PUT"];
NSString *input=[NSString stringWithFormat:#"abc[image]=%#",base64encodedString];
[request setHTTPBody:[input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];
NSDictionary *statuses = [parser objectWithString:json_string error:nil];
[parser release];
[json_string release];
return statuses;
I am getting error message from the server side like this: "xxxxxx.jpg is not recognized by the 'identify' command."
Kindly suggest a solution.
At last i found that the content type should be changed to "multipart/form-data" from "application/x-www-form-urlencoded". Can anyone please tell me how pass Base encoded string using this way? Pls...i am behind this issue for the last 20 days...
There could be a difference in the Base64 routine. Base64 encode the same data on the server and the iPhone and compare.
Using the simulator use a network snooper such as Wireshark on the Mac to see what is really going out and the full return from the server. If you are using SSL you can use the Mac app Charles to see the data.
Related
I'm pretty new to everything that deals with http and stuff like that and i know almost nothing about how http methods work so i need your advice. In my app i have to send login and password to a server and get a response. By now i'm trying to write a method (it's called STUB in English i believe) which will work in a general way without communicating to actual server. Should i use POST method in a manner similar to:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:some_server_url];
[request addPostValue:passwordString forKey:loginString];
or something else? if first how do i receive the response from the server? Could you please show me few lines of code?
You can pass login details to server using HTTP POST method like this :
-(NSDictionary*)userLogIn:(NSMutableDictionary*)inputDictionary{
SBJSON *parser = [[SBJSON alloc] init];
NSString *convertedString=[NSString stringWithFormat:#"login=%#&password=%#",username, password];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"Give the url here"]];
[request setHTTPBody:[convertedString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"XXXX" forHTTPHeaderField:#"USER_AGENT"];
[request setHTTPMethod:#"POST"];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];
NSDictionary *statuses = [parser objectWithString:json_string error:nil];
[parser release];
[json_string release];
return statuses;
}
This is a sample code and you have to change according to your requirements.
i am implementing this code wich take apple in app purchase receipt from the current transaction (not listed here) i am converting it to base64 NSData object, create new NSString with the values and keys (json object) and send it trough NSUrlconnection .
when the compiler hits the init with request the app crashes after 2 seconeds...
without to get any response. this is the code.
NSData *data = [NSData dataFromBase64String:receiptStr];
NSString *jsonString = [NSString stringWithFormat:#"{\"receipt-data\":\"(%#)\",\"password\":\"(%#)\"}", data, SHARED_SECRET];
NSLog(#"%#",jsonString);
savedReceipt = jsonString;
[[NSUserDefaults standardUserDefaults]setValue:savedReceipt forKey:#"savedrecipt"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSData *requestdata = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; //urlData = [[NSMutableData data] retain];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"https://sandbox.itunes.apple.com/verifyReceipt"]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[NSString stringWithFormat:#"%#",requestdata]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
anyone have an idea what am i doing wrong?
i am also new to json so it could be also a problem there.
That's because this line in your code:
[request setHTTPBody:[NSString stringWithFormat:#"%#",requestdata]];
Is trying to setHTTPBody to some formatted NSString, when the method is actually expecting NSData.
Just use:
[request setHTTPBody: requestdata];
And see if you have better results.
I am new to iPhone application development and also this stackoverflow,if I made any mistakes sorry. I have some of the questions
1) Do I need to install any other external framework into my application. I am using SDK Simulator 4.2
2) I have gone through some of the material which was posted here, I found some code and I made some modification to suits my application When I am requesting for a URL it is returning me NULL.
3) My task is to send a three parameters into database.
NSString *post =[NSString stringWithFormat:#"?FirstName=%#?Surname=%#?IDNumber=%#?DOB=%#",namestring,surnamestring,cidstring,dobstring];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSLog(#"Post Length:",[postData length]);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://www.esoft.co.za/MDS/Service1.asmx?op=InsertSuppliers"]];
[request setHTTPMethod:#"GET"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(#"response data is: %#",[responseData length] );
NSString *data = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"Data: %#",data);
Thanks in Advance Guys...Please provide me any sample code or material..
I think it's because you're trying to send invalid request. Just try to check and log error message to see what's wrong.
What you're trying to do is to send POST data over GET request, which is wrong anyway. I'm also not sure about those "Post" data format, but when you'll check error message you'll find what's wrong.
NSLog(#"Request failed with error: %#", [err localizedDescription]);
best
I've added Facebook Connect to my new iPhone application. Everything perfect, no problem.
In this application, however, I need to post on user's Wall without prompt any dialog box.
I've searched in Facebook Documentation and, from what I understand, if I ask user to give me the right permission (in this case, publish_stream), the dialog box should no longer appear.
But the box appears, despite of all.
I hope you can help me.
Thank you.
P.S. Sorry for my bad English
Use the graph API after you have acquired the publish_stream permission. You make a POST to:
https://graph.facebook.com/ID/feed
This iPhone SDK does not support this natively, you will have to implement this yourself. You will need to ensure that you create the proper JSON encoded parameters and ensure they are properly escaped.
A good place to start with this is here.
Thank you!
So, if I understand well, I have to do something like this:
Use this framework http://code.google.com/p/json-framework/ to add JSON support.
And this code:
SBJSON *json = [SBJSON new];
json.humanReadable = YES;
NSString *service = #"NameService";
NSMutableDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
#"MessaggeFromMyApp",#"message",
#"http://www.sample.com",#"link",
#"nomeOfTheLink",#"name",
#"captionOfTheLink",#"caption",
#"descriptionofTheLink",#"description",
#"MyDistrict",#"value",
#"2",#"txs_Action",
nil];
//Pass it twice to escape quotes
NSString *jsonString = [NSString stringWithFormat:#"%#", [params JSONFragment], nil];
NSString *changeJSON = [NSString stringWithFormat:#"%#", [jsonString JSONFragment], nil];
NSLog(jsonString);
NSLog(changeJSON);
NSString *requestString = [NSString stringWithFormat:#"{\"id\":15,\"method\":\"%#\",\"params\":[%#]}",service,changeJSON,nil];
NSLog(requestString);
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: #"https://graph.facebook.com/me/feed"]];
NSString *postLength = [NSString stringWithFormat:#"%d", [requestData length]];
[request setHTTPMethod: #"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody: requestData];
//Data returned by WebService
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(returnString);
Thank you again.
I am new in iphone,in my app required data from the server.
1)mysql database uploaded on the server
2)With the help of php code i fetch the data from the server
problem is that how i integrate php in my iphone please solve my problem,
give any reference.
Thanks,
I'm not sure to understand your problem, but you can't use PHP on iPhone.
If you need data from an external database, create web services to get the data in JSON, XML, or what suits you best.
Your iPhone application will connect to a specific URL, and get the data from a (PHP) script that will query the database and return the result in a specific format.
The best way in my opinion is to use JSON (see http://code.google.com/p/json-framework/).
You can make requests to your server using this code
NSError *error;
NSString *post =[NSString stringWithFormat:#"any-data=%#",
myData];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://your-server.com/your-script.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"Data: %#",data);
NSDictionary* parsedData = [data JSONValue];
You then can access the data using
[parsedData valueForKeyPath:#"myVar"]
Hope this hepls,
Miguel
You could look at passing arrays of objects from the server to the iPhone in the form of a plist file. Have the server generate the plist in the appropriate format on a url and then on the device:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:#"http://www.example.com/myplist.php"];
NSArray *array = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:#"http://www.example.com/myplist.php"];
If your unsure what the format is for plist files create one in your xcode project and use that as a template.