0

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.

1
  • What is it supposed to log? because thats logging exactly what you are creating? I suspect you are not understanding what skills[][] is actually creating Commented Mar 5, 2016 at 6:42

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi Sir, thanks for your answer, I will keep this answer for the backup plan, Cause I have more than 5 Activities, and i will prefer if i get the solution from using JSON
it's the simplest way, trust me. GSON is the most powerful solution
I'll give it a try then :)
you can use Volley or Spring to get data, and work with Gson for calculate and scrolling data
yup, i already used Volley, i think it's all about format of the data sent to the server
|
1

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

Hi, thanks for your answer, May I ask you something? Is it the same with password=admin (Android) and "password" : "admin" (PHP Server)
no it is different. "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
Hi, do you mind to look my edited post above, I posted the hash values when I debug the code. Does the format correct? Cause I still got the error
If you use volley, you can try to send 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.
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.