Debojyoti Debojyoti - 4 months ago 12
JSON Question

How to post array to a php server in android?

I am using DataOutputStream class to send my data as post data to a php server

DataOutputStream dataOutputStreamRegister = new DataOutputStream(connectionToRegister.getOutputStream());
dataOutputStreamRegister.writeBytes("txtUserID="+userName+"&txtPassword="+userPassword+"&ddlCountry="+countryId
+"&ddlUniversity="+universityId+"&ddlCourseYear="+courseYearId+"&txtEmail="+userEmail+"&txtInitial="+userInitials
+"&txtFname="+userFirstName+"&txtLname="+userLastName+"&txtDob="+userBirthMonth+"/"+userBirthDay+"/"+userBirthYear
+"&ddlLevel="+levelId+"&txtSkills="+studentSkills+"&ddlGender="+genderId+"&ddlFraternity="+fraternityId+"&ddlSorority="
+sororityId+"&ddlClubs="+paramsText);


The last parameter "ddlClubs" i want to send should be like an array.The code in the php server is as follows

foreach($_POST["ddlClubs"] as $key=>$val){
....
}


How should i go about doing this as many techniques i saw included deprecated classes like "defaultHttpClient" and "NameValuePairs"

Thanks everyone for your time.

Answer

Just format your url accordingly:

...&ddlClubs[0]=value0&ddlClubs[1]=value1

This also works with associative arrays, such as:

&ddlClubs[name0]=value0&ddlClubs[name1]=value1

Unfortunately this has its limits as a url has a maximum length, meaning you cannot put a very huge array into the url.