0

I have sample code for sending key pair values as array list on HTTTP restful web service using POST method like below.

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

How to implement the same in iPhone Objective-C. any idea appreciated...

Shanmugaraja G
  • 2,778
  • 4
  • 31
  • 47
  • 1
    Read the documentation of [`NSURLRequest`](https://developer.apple.com/library/iOS/#documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html) or use the simple wrapper like [AFNetworking](https://github.com/AFNetworking/AFNetworking) – rckoenes Sep 14 '12 at 11:46
  • Possible duplicate: http://stackoverflow.com/questions/3566516/simple-http-post-example-in-xcode – Mazyod Sep 14 '12 at 12:03

2 Answers2

0

Include JSON library in your project ,you need to break down key-value pair into JSOn fragments and then send it via POST..

 NSMutableDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                               @"12345",@"id",
                                               @"Hi",@"stringData",
                                                 ,nil]; 




            //Pass it twice to escape quotes
            NSString *jsonString = [NSString stringWithFormat:@"%@", [dictionary JSONFragment], nil];
            NSString *requestString = [NSString stringWithFormat:@"%@",jsonString,nil];
            NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
            NSString *urlstr=[NSString stringWithFormat:@"your URL"];

            str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSURL *url=[NSURL URLWithString:str];

            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
            NSURLResponse *response = nil;
            NSError *error = nil;

            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/json" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPBody: requestData];

            NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error ];
            NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];

    NSLog(@"your response %@",returnString);
AppleDelegate
  • 4,269
  • 1
  • 20
  • 27
0

I wrote a decent network client that you are welcome to use. It uses AFNetworking for the processing and works very well. Take a look HERE.

Community
  • 1
  • 1
LJ Wilson
  • 14,445
  • 5
  • 38
  • 62