0

i am post the data using webservie but data is not post into database.`

    NSURL *url = [NSURL URLWithString:@"some url"];
    NSArray *res=[[NSArray alloc] initWithObjects:txtname.text, nil];
    NSError *jsonError;
    //NSJSONSerialization is Apple's new json serialization class so we can use it to convert to and from json and foundation objects
    NSData *requestdata = [NSJSONSerialization dataWithJSONObject:res options:0 error:&jsonError];


    NSMutableURLRequest *request;
    request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestdata length]] forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:requestdata];

    //this kicks off the request asynchronously
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

`
Paras Joshi
  • 19,954
  • 11
  • 54
  • 69
Mistry Mehul
  • 21
  • 1
  • 5
  • Look at http://stackoverflow.com/questions/4456966/how-to-send-json-data-in-the-http-request-using-nsurlrequest and http://stackoverflow.com/questions/4085978/json-post-request-on-the-iphone-using-https. Also try to replace "application/x-www-form-urlencoded" with "application/json" – βhargavḯ Apr 19 '13 at 04:23
  • i have only now 1 textbox and that textbox value i want to send to database through json ?how? – Mistry Mehul Apr 19 '13 at 04:28
  • Please refer above links and Make sure to have valid json while requesting. – βhargavḯ Apr 19 '13 at 04:30
  • but i am a new in ios and json is completly new ..plz help for that...how do that? – Mistry Mehul Apr 19 '13 at 04:32
  • Try debugging your web-service first with some REST-debugger. I suggest Chrome extension named Postman. Sometimes, iOS code is completely correct and there is a bug in a web-service. – Vladimir Obrizan Apr 19 '13 at 05:44
  • By the way, which error do you get in NSURLConnectionDelegate? – Vladimir Obrizan Apr 19 '13 at 05:45

1 Answers1

0
NSString *parameter1=@"";
NSString *parameter2=@"";  

NSString *urlString = [NSString stringWithFormat:@"your URL"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSMutableData *body = [NSMutableData data];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

//  parameter parameter1
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter_1\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:parameter1
 dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

//  parameter parameter2
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter_2\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:parameter2 dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


// setting the body of the post to the reqeust
[request setHTTPBody:body];

// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"returnString %@",returnString);

In the Similar manner you can add as many paramters as you want .

Gaurav Rastogi
  • 2,105
  • 1
  • 12
  • 19