0

I have a whole suite of PHP scripts that interact with both the Android and iOS version of a mobile app. They all work the same: After the mobile app initiates a GET or POST, the PHP script typically returns a dash delimited string.

e.g. If I want to get a list of the comments on a particular page, the PHP script would return something like

user1-comment1-user2-comment2

Is there a better way than this? Because if I ever want to return a new variable e.g.

user1-comment1-newValue1-user2-comment2-newValue2

then this will break all current versions of the mobile app.

yhl
  • 669
  • 1
  • 6
  • 18
  • 2
    use standard format like `json` ... – Baba Jul 02 '13 at 16:46
  • 1
    can you have the return value be a json encoded string of a dictionary (ie. php array with keys and values)? both android and ios have support for json decoding and encoding. alternatively, if you want to save a few bytes of space you can look at messagepack. it's like json but smaller: http://msgpack.org/ – Mr. T Jul 02 '13 at 16:46

4 Answers4

1

Why not serialize the result and parse it in java? You could also use json_encode in PHP and decode it in your android app... see How to parse JSON in Android

Community
  • 1
  • 1
Rob W
  • 9,134
  • 1
  • 30
  • 50
1

You need to use a serializer. If you have a user name that contains a - you'll run into problems. Serializers take care of this for you. The current favorite is JSON, used to be XML.

JSON has excellent support in most web languages.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
1

You can serialize your array of data into either a json string or a message pack string. Let's say your php array was:

$a = array(
    "user1" => "comment1",
    "user2" => "comment2",
    "user3" => "comment3"
);

That would translate to this in json:

{
    "user1": "comment1",
    "user2": "comment2",
    "user3": "comment3"
}

This json string can be easily converted to NSDictionaries on ios (using NSJSONSerialization) and JSONObjects on android (tutorial here).

A message packed string would be similar in structure to the json string above, but is less human readable because of its more compact nature. However, both Java and Objective-C have libraries to help translate messaged packed data into native objects.

Community
  • 1
  • 1
Mr. T
  • 12,795
  • 5
  • 39
  • 47
1

Using JSON, you can make use of name/value pairs, so the order or inclusion of the parameters won't matter. JSON also provides hierarchy and limited typing, such as number versus string.

JSON also allows you to easily escape characters, so you could have any value you want (even with backslashes and quotes.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143