I used a code to post a comment to a wordpress blog from the following question:
Post comment to WordPress Blog from iPhone programmatically
I made a few modifications to get text from UITextFields:
- (IBAction)postComment:(id)sender {
NSString *post_url = #"http://movilarena.com/wp-comments-post.php";
NSString *post_content = #"comment_post_ID=%#&comment_parent=%#&author=%#&email=%#&comment=%#";
NSString *post_str = [NSString stringWithFormat:post_content, #"1", #"0", self.txtName.text, self.txtEmail.text, self.txtComment.text];
NSData *data = [NSData dataWithBytes:[post_str UTF8String] length:[post_str length]];
NSURL * url = [NSURL URLWithString:post_url];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
[req setHTTPMethod:#"POST"];
[req setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
[req setHTTPBody:data];
//Synchronous
NSURLResponse *response;
NSError *err;
NSData *ret = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&err];
NSString *retString = [NSString stringWithUTF8String:[ret bytes]];
NSLog(#"%#",retString);
}
My problem is that the sendSynchronousRequest returns an error:
Execution was interrupted, reason: EXC_BAD_ACCESS (code=1,
address=0x0). The process has been returned to the state before
execution.
I will appreciate any suggestions to identify what's wrong here. I use XCode 4.5.2 and run the code on an iPhone 4S with iOS 6.0.1
Solved myself. The field comment_post_ID should be the ID of the post I am trying to reply.
NSString *post_content = #"comment_post_ID=%d&comment_parent=%#&author=%#&email=%#&comment=%#";
NSString *post_str = [NSString stringWithFormat:post_content, self.postID, #"0", self.txtName.text, self.txtEmail.text, self.txtComment.text];
Related
This is my first IOS project. Having a tough time getting login to work. The site has a meta_refresh to another url. I've tried to send another request to the url in the meta_refresh but then the app just hangs. I'm sure that I'm doing something wrong but I'm using XCode 4.4 so a lot of the NSURLConnection delegate methods have been deprecated.
Here's what I'm doing:
NSString *urlAsString = baseURL;
urlAsString = [urlAsString stringByAppendingString:#"?user[login]=username"];
urlAsString = [urlAsString stringByAppendingString:#"&user[password]=password"];
urlAsString = [urlAsString stringByAppendingString:#"&origin=splash"];
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:#"POST"];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
if([data length] > 0 && error == nil){
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"HTML = %#", html);
NSRange obj = [html rangeOfString:#"http-equiv=\"refresh\""];
NSInteger len = obj.length;
} else if([data length] == 0 && error == nil) {
NSLog(#"No data was returned.");
} else {
NSLog(#"Error happened = %#", error);
}
Here's what I'm getting:
Another question is that I've read that I should use asynchronous connection. The trouble is that I need the data scraped from the site to display on the screen. If I display the screen before the data is returned, then I won't have any data -- or am I not understanding how this works?
Thanks.
--Tony
I think that maybe you're a mixing stuff. You're setting a string url with GET values and then you set the HTTPMethod to POST and I don't know if that is going to work fine.
Here's a code that I have to send values with POST:
NSString *post = [NSString stringWithFormat:#"&user_login=%#&user_password=%#&origin=%#,username,password, splash];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:baseURL]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
I am trying to post a string to a URL in iphone which will be retrieved by the server. The server side scripting is done in coldfusion. I am trying to fetch the data in coldfusion and pass on the value back to iphone. I am not familiar much with coldfusion and wanted some help in this. Below is the coding in iphone :
/** Iphone code **/
NSString *post =[[NSString alloc] initWithString:#"Hello World"];
NSURL *url=[NSURL URLWithString:#"http://www.abc.com/test.cfm"];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
/*Coldfusion code */
<html>
<head><title>Test</title></head>
<body>
<cfoutput>#form.post#</cfoutput>
</body>
</html>
Am I doing it correctly ?
You may need to specify name of form variable in data. Try below for post variable. I haven't tested it should work.
NSString *post =[[NSString alloc] initWithString:#"post=Hello%20World"];
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'am working on a iPad project and this project needs to talk to a json-rpc webservices. The webservices is based on Drupal with module : cck and views
1)I need to push a json object into the webservice
2)I need the callback data from the webservice
I already have implemented the SBJSON api and the https://github.com/samuraisam/DeferredKit/ api to the iPad project.
The SBJSON api works fine and I understand this one
The Samuriaisam DefferedKit is new for me
My question is how to get data out of this json-rpc webservice, has someone some sample code? Or some places where I can find Objective C - json-rpc webservice documentation.
Kind Regards,
Bart Schoon
---------Update--------
I use this code now:
NSString *jsonString = #"{\"method\":\"views.get\",\"params\":{\"view_name\":\"client_list\",\"sessid\":\"xxxxxx\"},\"id\":1}";
NSString *requestString = [NSString stringWithFormat:#"%#",jsonString,nil];
NSLog(#"input: %#",jsonString);
NSData *requestData = [NSData dataWithBytes: [jsonString UTF8String] length: [jsonString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:#"http://subdomain.domain.com/services/json-rpc"]];
NSString *postLength = [NSString stringWithFormat:#"%d", [requestData length]];
[request setHTTPMethod: #"POST"];
[request setValue:#"Content-type: application/json" 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(#"output: %#",returnString);
This will result in this message from the server:
{"error":{"name":"JSONRPCError","code":-32600,"message":"The received JSON not a valid JSON-RPC Request"},"version":"1.1"}
---------/Update--------
What is wrong? Has someone experience with this?
Kind Regards,
Bart Schoon
Read JSon file get that data.
NSDictionary *dictionary = [jsonString JSONValue];
You will get key & value pair. Store that data in your respective variable.
-(IBAction)testCall{
NSString *requestString = [NSString stringWithFormat:#"method=views.get&view_name=client_list",nil];
NSLog(requestString);
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: #"http://.xxxxxxxxx.nl/services/json"]];
NSString *postLength = [NSString stringWithFormat:#"%d", [requestData length]];
[request setHTTPMethod: #"POST"];
[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);
NSDictionary *dict = [returnString JSONValue];
}
Just don't use a json-rpc - keep it simple and json the normal jSon method ;)
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.