-1

I made my first RESTful webservice, which just need to get an integer value and it will return a dataset of my MySQL database. For example if I send a 2 get the this response:

[{"Player_ID":"2","Login":"Test123","Passwort":"***********"}]

Can someone explain how I can post an int value in objective c and also explain how I can work with that MySQL dataset? For example set label01.text to the Login "test123".

Would be great if someone can help me, working with a webservice is new for me.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Hakeemo
  • 3
  • 1
  • To be honest, not that much. I dont understand how to set the post to an integer . NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:@"http://192.168.2.112getdata.php"]]; [request setHTTPMethod:@"POST"]; – Hakeemo Nov 21 '13 at 17:02

2 Answers2

1

As far as posting the int goes, ilya n.'s answer should work.

Here is how you would work with the data you get back:

Here's NSJSONSerialization documentation for reference: NSJSONSerialization Class Documentation.

Basically, the only two types of Obj-C objects that can be serialized an NSDictionary and NSArray. If you are needing to SEND JSON data, what you will probably want to do is something like this:

NSNumber* loginId = @1234;
NSDictionary* dict = @{@"Login" : loginId};
NSError* err = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&err];
NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

This will give you a JSON-formatted NSString that you can pass off to something expecting something serialized using JSON:

jsonString == @{@"Login":"1234"}

If you are RECEIVING a JSON string from your webservice that you need to do something with in Obj-C, you can do the following:

NSString* jsonString = theJSONstringYouWereSentFromWherever;
NSError* err = nil;
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id data = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&err];

At this point, data with either be an NSArray or an NSDictionary. You will need to check it figure out what it is and what you need to do with it. Keep in mind that you could have a dictionary containing more dictionaries and/or arrays, or and array containing more arrays and/or dictionaries. JSONObjectWithData will turn whatever you got into some matching Objecive-C data structure.

For your example, you would get the string @"[{"Player_ID":"2","Login":"Test123","Passwort":"*****"}]"

Deserializing, you would wind up with the following NSDictionary:

@{"Player_ID":"2","Login":"Test123","Passwort":"***********"}

You would then be able to do:

label01.text = [dict objectForKey:@"Login"];

Hope that's what you were looking for.

Jordan
  • 4,133
  • 1
  • 27
  • 43
0

From your question, I believe you'll be best served by one of the existing frameworks.

As per iOS Most mature REST service library, try RESTKit.

Of course, you can also form request manually:

[request setBody:[NSString stringWithFormat:@"%d", integerValue]];
Community
  • 1
  • 1
ilya n.
  • 18,398
  • 15
  • 71
  • 89