I have my Android Java code that have to pass some array and some multidimensional array to the PHP Server. Unfortunately, I don't have the access to the PHP Server.
Here's the PHP Server API :
$result= array(
"username" => "admin",
"password" => "admin",
"hobbies" => array(
"1" => "science"
),
"skills" => array(
"1" => array(
"Boxing" => "Yes"
)
)
);
Here's my Android Java Code :
final String hobbies[] = {"science"};
final String skills[][] = {{"Boxing"},{"Yes"}};
@Override
protected Map<String, String> getParams() {
Map<String, String> jParams= new HashMap<String, String>();
JSONArray arrHobbies = new JSONArray();
JSONArray arrSkills = new JSONArray();
arrHobbies.put(hobbies[0]);
arrSkills.put(skills[0][0]);
jParams.put("username", "admin");
jParams.put("password", "admin");
jParams.put("hobbies",arrHobbies.toString();
jParams.put("skills",arrSkills.toString();
return jParams;
}};
Then, I use this code to see how the data've been sent.
Log.d("Response : ", jParams.toString());
It shows this on the Android Log :
{password=admin, username=admin, hobbies=["science"], skills=[["275]]}
UPDATE Here's the hash values when I debug the code:
0 = {java.util.HashMap$HashMapEntry@4884} "hobbies" -> "{"1":"1"}"
1 = {java.util.HashMap$HashMapEntry@4885} "password" -> "admin"
2 = {java.util.HashMap$HashMapEntry@4886} "username" -> "admin"
3 = {java.util.HashMap$HashMapEntry@4887} "skills" -> "{"1":{"Boxing":"Yes"}}"
I never succeed to communicate with the PHP Server API. Please help me where am I missing or wrong.
3 Answers 3
there is a library how call GSON, search it on internet it's the fastest and simplest way to resolve String to Json and Json to String.
6 Comments
I think your problem is that associative array in php represents JSON object in your json string so you probably should send something like this to your PHP Server API:
{
"password": "admin",
"username": "admin",
"hobbies":{
"1": "science"
},
"skills": {
"1": {
"Boxing": "Yes"
}
}
}
Here i can't be 100% sure and it depends on your server implementation, but to achieve the json output from above, your getParam() should look like this:
protected String getParams() {
JSONObject jParams = new JSONObject();
try {
JSONObject arrHobbies = new JSONObject();
arrHobbies.put("1", "science");
JSONObject boxing = new JSONObject();
boxing.put("Boxing", "Yes");
JSONObject arrSkills = new JSONObject();
arrSkills.put("1", boxing);
jParams.put("username", "admin");
jParams.put("password", "admin");
jParams.put("hobbies", arrHobbies);
jParams.put("skills", arrSkills);
} catch (JSONException e) {
e.printStackTrace();
}
return jParams.toString();
}
4 Comments
"password" : "admin" is json representation of key => value and if you put password=admin in json it will be invalid. pro.jsonlint.com here is nice tool to check json validity. You PHP server will probably try to just use this function to decode your json. And here is also how i got the json you server probably asks: LINK JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,URL, new JsonObject(hashMap), instead of HashMap. I usualy do similar tasks this way. 1) install postman 2) make sure it works in postman so you 100% sure what server expects from you. 3) create JSONObject that will produce the same json string as your postman 4) play with Volley or Retrofit to send your json.Hope this may help you :
@Override protected Map getParams() {
Map<String, String> jParams = new HashMap<String, String>();
JSONArray arrHobbies = new JSONArray();
JSONArray arrSkills1 = new JSONArray();
JSONArray arrSkills2 = new JSONArray();
arrHobbies.put("science");
arrSkills1.put("Boxing");
arrSkills1.put("Yes");
arrSkills2.put(arrSkills1);
jParams.put("username", "admin");
jParams.put("password", "admin");
jParams.put("hobbies", arrHobbies.toString());
jParams.put("skills", arrSkills2.toString());
return jParams;
}
Comments
Explore related questions
See similar questions with these tags.
skills[][]is actually creating