0

I'm creating a iPhone app, and im trying to figure out how to create a JSON request to the webservice that contains parameters. In Java this would look like this

HashMap<String, String> headers = new HashMap<String, String>();

JSONObject json = new JSONObject();

            json.put("nid", null);
            json.put("vocab", null);
            json.put("inturl", testoverview);
            json.put("mail", username); // something@gmail.com
            json.put("md5pw", password); // donkeykong

        headers.put("X-FBR-App", json.toString());

The header has to contain a JSON object and "X-FBR-App" before the service recognizes the request. How would this be implemented in Objective-C ?

Lahib
  • 1,196
  • 5
  • 29
  • 60

2 Answers2

1

USE Post method.try this

    NSString *method =  @"addProduct"; 
 NSString *jsonstring=@"http://yourdomain.com/product_review_api/product-review.php?"; 
 NSString *urlString = [NSString stringWithFormat:@"%@",jsonstring];
 NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
 [request setURL:[NSURL URLWithString:urlString]];
 [request setHTTPMethod:@"POST"]; 
  NSMutableData *body = [NSMutableData data];
    // image file
   NSData *imageData = UIImageJPEGRepresentation( labelimage, 0.8);
   NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

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

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

[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);
    NSDictionary *profile = (NSDictionary*)[returnString JSONValue];
    NSLog(@"profile=%@", profile); 

you can append all the parameters like this.

iOS Developer
  • 1,713
  • 2
  • 16
  • 47
  • Thanks for the quick response. I have one question so far. In the code you have sent, my Content-Type would be -> "X-FBR-App" ? – Lahib Apr 10 '12 at 12:22
  • try replacing content type with "X-FBR-App" – iOS Developer Apr 10 '12 at 12:29
  • see this http://stackoverflow.com/questions/4085978/json-post-request-on-the-iphone-using-https – iOS Developer Apr 10 '12 at 12:42
  • thanks for the link. In the code u sent, at this line ->> "NSDictionary *profile = (NSDictionary*)[returnString JSONValue];" i cant get hold of the JSONVALUE. Dont know if you can gues why ? – Lahib Apr 10 '12 at 13:32
  • are u getting the returnString? – iOS Developer Apr 11 '12 at 04:02
  • Yeah, i can reach the returnString veriable, but i get an error when trying to reach the JSONVALUE. This Image shows the error http://prntscr.com/7zxnh – Lahib Apr 11 '12 at 13:12
  • NSDictionary *profile = (NSDictionary *)[returnString JSONValue]; – Lahib Apr 12 '12 at 10:25
  • I found the solution, i have to declare it this way: {NSError *error; NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *results = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];} – Lahib Apr 13 '12 at 00:28
  • @Lahib sorry for the late reply..anyway happy to knw you found the solution. – iOS Developer Apr 18 '12 at 04:05
1

JSON isn't baked into iOS, but if you look at www.JSON.org there are a number of Objective-C frameworks that provide the required functionality. I have no experience with any of them so I can't make recommendations, but it is a good start to finding a solution.

Also look at this SO question post-data-through-json-webservices-in-iphone

Community
  • 1
  • 1
Peter M
  • 7,145
  • 3
  • 45
  • 86
  • As of iOS 5.0 and OS X 10.7 JSON is baked into Cocoa http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html – JeremyP Apr 10 '12 at 14:13