Raikoug Raikoug - 1 year ago 130
reST (reStructuredText) Question

RestSharp android upload multiple lines

I can use RestSharp very well sending "mono-line" things like:

"user, password, email, telephone, ... "

But I can't understand how to send multiple lines, for example, all the values in this table:

| Dog | Race | user |
| Skitty | Doberman | User1 |
| Birillo | Pinscher | User2 |
| Fragolino | Corgi | User3 |
| ... | ... | ... |

How am I supposed to format parameters for restsharp?


Edit: as asked I show How I send "normal" data:

I take data from user input in EditText (like, string1, string2, string3), and send them to a class I use for RestSharp, and I send them with:

var client = new RestClient("x.x.x.x/app/");
var request = new RestRequest("/ServiceX", Method.POST);
request.AddParameter("Dog", string1);
request.AddParameter("Race", string2);
request.AddParameter("User", string3);
IRestResponse response = client.Execute(request);
var content = response.Content;

This is a single line in a table, I can add only 1 line per request in this way. But I want to upload more lines.


Answer Source

This is all the code i came up with:

In the c# app, i've a class storing the data, like

public class MyClass
        public string field1{ get; set; }
        public string field2 { get; set; }
        public string field3 { get; set; }
        public string field4 { get; set; }

Then I populate a List with all my entries:

List<MyClass> list= new List<MyClass>();
// make new element of MyClass like "element1"

Well, this method is awesome because list can be populated very easily with a foreach or a while, taking all edittext in the layout, even dynamics ones, awesome.

After this next part require "Newtonsoft.Json" (NutGet is you friend... your BEST friend!)

 string json = JsonConvert.SerializeObject(list);

Well now you have to send it, I use post like I wrote in first post.

In the php part i took the data and parsed with:

$data = (json_decode($data));

This worked like a charm, becouse I can access at elements with:

foreach ($data as &$object){

Well now just build the query string and it's over... (just for my own desire for completeness, i paste the last part)

$query = 'INSERT INTO `table_name`(`field1`, `field2`, `field3`, `field4`) VALUES ';
foreach ($data as &$object)
        $query_parts[] = "('" . $object->field1 . "', '" . $object->field2 . "', '" . $object->field3 . "', '" . $object->field4 . "')";

        $query .= implode(',', $query_parts);


Thanks @mwisnicki for the help. MANY thanks!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download