简体   繁体   中英

How to Send an Array Over HTTPPost

I have to send these parameters over httppost. From my android app to a drupal server.

This is the php representation

$data = array(
        'node' => array(
            'type' => 'celapp_order',
            'title' => 'order celeb: nixon-fan '.date("j.m.Y H:i:s"),
            'field_fan_uid' => array(0 => array('uid' => 4)),
            'field_celeb_uid' => array(0 => array('uid' => 5)),
            'field_celeb_price' => array(0 => array('value' => 0.79))
        )
    );

In the past I have used namevaluepairs. I cannot work out how to send the parameters using namevaluepairs. If anybody can help I will be very greatful

Use a JSON wrapper:

$postable = json_encode($data); // in PHP, there's equivalents for Java out there.

That'll convert the array into plaintext (basically javascript) which is easy to send via POST. the receiving end would use json_decode() (or whatever its equivalent is on that particular platform) to convert from the JSON version back to a native array/object.

I've used GSON with great results for serializing/deserializing JSON ro and from plain old java objects (POJOs). Link to array examples here .

eg:

public class Foo {

    private string Bar;
    private int Num;
}

Foo[] foos = new Foo[] ... // create array here

Gson gson = new Gson();
String text = gson.toJson(foos);

/*
output: 

[
  {
     "Bar" : "Hello",
     "Num" : 25
  },
  {
     "Bar" : "World",
     "Num" : 42,
  }
]

*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM