Waseem Akram February 2016

I want to post array param in HttpURLConnection android

I am a fresher in developing Android apps, in my app I want to post params in the format below:

user_id=12&task_id=100&user_ids=["91","92"]

I am not able to send the user_ids in array format. I am receiving the user_ids as string in my Rails API.

I am using HttpURLConnection, and in my writer.write() I am passing my post params as

writer.write("user_id="+id+"&task_id="+tid+"&user_ids="+["91","92"]+");

Please help.

Answers


Waseem Akram February 2016

If any one facing the same problem, I found a work around. You need to add the array in a string

1. Step 1

Create a final ArrayList<String> SelectedTagIds = new ArrayList<String>();

and add the array items using

SelectedTagIds.add("One");
SelectedTagIds.add("Two");
...

2. Step 2

Convert

SelectedTagIds.toString();

3. Step 3

Format the String by replacing the string to

SelectedTagIds.replace("[","").replace("]","").replaceAll("\\s","").trim();

4 Step 4

Finally post the String to the server (I used HttpURLConnection to post my data to the server), and in the server code, convert the received SelectedTagIds to Array and loop accordingly to achieve you'r desired result.

Post Status

Asked in February 2016
Viewed 2,386 times
Voted 9
Answered 1 times

Search




Leave an answer