How to send the updated data using json to server in iphone? - iphone

am using web services (JSON). from json am getting data this data loading into tableview am trying to edit this data but after edit the data how to send this updated data to server.
please any one help me?

try this will help you.. this is the post method for updating data in WS .
NSString *post =[NSString stringWithFormat:#"uid=%#&firstname=%#&lastname=%#&phone=%#&bday=%#&about_me=%#&image=%#&image_code=%#&contact_number=%#",LoginID,fname,lname,cn,bday,abtme,strimage11,c11,cn];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"YOUR LINK"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *uData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:uData encoding:NSUTF8StringEncoding];
//
NSMutableDictionary *temp = [data JSONValue];
//
NSLog(#"%#",temp);

I am assuming you have your edited data, then
Form your json. There are several libraries out there which can help you.
Know the hostname of your server.
Know which API to hit on your server.
then pass this json as POST (GET is also ok, but POST is preferred).
Process this received json on your server.
Hope this helps. Nothing much to it actually.

Related

How to post multiple data to web server

I have below code for posting data to the web server but it gives me null response, I have multiple data and they are:-
date,
rig,
driller,
offsider-1,
offsider-2,
shift
,
project,
supervisoremail,
geologistemail,
comments,
plants[0][name-id],
plants[0][start-usage],
plants[0][end-usage],
consumables[0][category],
consumables[0][name-id],
consumables[0][used],
consumables[0][code],
consumables[0][description],
I have to post this data and I used this code for posting but it didn't work for me. Please help me out.
NSURL *url=[NSURL URLWithString:#"http://www.mydor.com.au/staff/dor/idorInsert"];
NSString *post =[[NSString alloc]initWithFormat:#"date=15/08/2012&rig=53&driller=207&offsider-1=131&offsider- 2=236&shift=day&project=24&supervisoremail=24&geologistemail=24&comments=&plants[0][name- id]=286&plants[0][start-usage]=1.0&plants[0][end-usage]=0.0"];
NSLog(post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding 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];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(#"here u re");
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"here also called");
NSLog(#"%#",data)
Thanks in advance.
Here is an article which demonstrates how to consume .NET Web service using iOS application. You can use the techniques discussed in the article:
http://www.highoncoding.com/Articles/809_Consuming__NET_Web_Services_in_an_iOS_Application.aspx
Almost all the work I do involves .NET with iOS. Have a look here for the network class I wrote and use all the time (AFNetworking based). I prefer to use a regular aspx form instead of an asmx web-service. I do this because I don't want to expose any web-methods to snooping visitors and I use an API key check before returning anything in the aspx form. I can send you an example .NET code (written in VB.NET) if you need me to.

How to use .NET web service in iPhone, with POST method?

I am communicating with a .NET web service in an iPhone application, using the POST method, but I am not getting XML back as response. Here is my code:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"Myserver/EventService.svc"]];
NSString *api = #"/registeruser";
NSData *body = [[NSString stringWithFormat:#"%#/%#/%#/%#/%#/%#/%#/%#/%#",
api, txtFName.text ,txtLName.text,
txtUName.text, txtEmail.text, txtPassword.text,
txtCity.text, txtState.text, str] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSLog(#"url %#",url);
NSLog(#"body %#",[NSString stringWithFormat:#"%#/%#/%#/%#/%#/%#/%#/%#/%#",
api, txtFName.text, txtLName.text,
txtUName.text, txtEmail.text, txtPassword.text,
txtCity.text, txtState.text, str]);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPBody:body];
[request setValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
NSLog(#"body len %d",[body length]);
NSString *postLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *XMLString=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"data %#",XMLString);
Please help me. Thanks in advance.
I have done that with following.
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"Myserver/EventService.svc/webservice/%#/%#",para1,para2]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue:0 forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *XMLString=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"data %#",XMLString);
Have you tried something smaller first? Say doing an http post locally? Or doing an http post to that server with a simple html form?
I have run into this issue myself and decided to step back and start with a simple http post to make sure my objective-c was correct first.
I used this tutorial to get me started -hope it helps

Fetch POST value in coldfusion from objective C

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

How to access a SOAP webservice from my iPhone app

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

Why this POST don't work?

this is my snippet:
NSString *post = #"from=A&name=B&email=ciccio#aaasd.com&messaggio=MESSAGE&codHidden=-1";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://www.mysite.com/page.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(#"Succeeded! Received %d bytes of data",[urlData length]);
NSString *outputdata = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
NSLog(#"%#", outputdata);
It call a php page that send me an email...
Is there any errors in this code?
Does not seem to work...
thanks
Is it a Synchronous/Asynchronous problem? If so, use the delegate-pattern and access the data asynchronously. Could you post any errors you see (or the server's response string)?
EDIT: Since you're not doing anything useful with the error returned, change your class to do the request asynchronously. A great answer to this was previously posted here. Once you implement didFailWithError you'll have a better picture.
Try to change NSASCIIStringEncoding to NSUTF8StringEncoding