0

Hello guys I'm trying to decode a JSON value into my Android Application, But I'm not really sure What Am I doing wrong in my code...

This is my PHP file:

<?php 

    ... 
    echo json_encode($mostrar_player);
    ...

?>

Which Will return me something like this: ["1","Admin","123","Adm","messages","0"]

in my Android Application I need to put that value into an Array there I can select values separeted, but how?

this is my Android Code code:

        EditText campoLogin = (EditText)findViewById(R.id.campo_de_login);
        EditText campoSenha = (EditText)findViewById(R.id.campo_de_senha);

        toast("Conectando...");
        Conectar Conn = new Conectar("http://website/chat/login.php?login=" + campoLogin.getText() + "&senha=" + campoSenha.getText() );
        Log.i("-RESPOSTA-",Conn.response); 
        // HERE IN Conn.response I have my response ["1","Admin","Admin","Admin","affs","0"]

        String json_str = Conn.response;
        JSONObject json = new JSONObject(json_str);
        JSONArray jArray = json.getJSONArray("msg");

        //Log.i("JSON",json_data.getString("msg"));
Dannark
  • 716
  • 6
  • 12
  • after the program reach the line Log.i("-RESPOSTA-",Conn.response); It Bugs, and them I get this: java.lang.IllegalStateException: Could not execute method of the activity – Dannark May 18 '14 at 19:36
  • I'm trying to put this ["1","Admin","123","Adm","messages","0"] in a array that I can manage. :) – Dannark May 18 '14 at 19:39
  • You are running HTTP request in main thread, Android does not allows that. Put it in separate thread. – Boban S. May 18 '14 at 19:40

3 Answers3

0

try

 <?php   echo json_encode($mostra_player, JSON_FORCE_OBJECT); ?> 

which forces to be an object.

Then you can access it in java/android with

JSONObject obj = (JSONObject) new JSONTokener(json_string_output).nextValue();
String yourdata= obj.getString("yourdata");

If you want an Array try

<?php echo json_encode(array("data" => $mostra_player)); ?>

        JSONObject json = new JSONObject(responsedata);
        JSONArray jArray = json.getJSONArray("posts");
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject e = jArray.getJSONObject(i);
            JSONObject jObject = new JSONObject(e.getString("data"));

            Log.d("yourdata", jObject.getString("yourdata"));
            Log.d("moredata", jObject.getString("moredata"));
        }
Emanuel
  • 8,027
  • 2
  • 37
  • 56
  • Hey Bro! Thats worked! :D you was right now my php is returning me this : {"0":"1","1":"Admin","2":"1234","3":"Adm","4":"message","5":"0"} with 0,1,2,3,4 e 5 which I believe is the number for the each column so I change this line: String yourdata = obj.getString("1"); to get the second column "Admin" ^^ – Dannark May 18 '14 at 20:04
  • You should consider trying json_encode(array($mostra_player)) if you want an Array in JSON. If my Answer helped you, do not forget to upvote. Thanks – Emanuel May 18 '14 at 20:06
  • if I use $mostra_player)); ?> it returns me {"data":null} D: – Dannark May 18 '14 at 21:22
  • look how it is : $query = "SELECT * FROM chat "; $res = mysql_query($query); $mostrar_player = mysql_fetch_row($res); echo json_encode(array("data" => $mostra_player)); – Dannark May 18 '14 at 21:23
  • Fixed it. And it's mysql_fetch_array to get a proper array instead of just one row. – Emanuel May 19 '14 at 08:34
0

There are a couple of options for you available to achieve this.

Personally i like to use the built in JSON parser in Android

JSONObject jObject = new JSONObject(result);

You can also use Google's json parse library https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html

Also see this link for details on how to actually parse JSON and get Strings that you can use in your program. How to parse JSON in Android

Community
  • 1
  • 1
Bob
  • 127
  • 1
  • 14
  • Bob, it seems that he is using a JSON Library already, else he wouldnt have methods like getJsonArray(). It appears to be the json.org parser which means that my answer is the proper way parsing json data from php to java. – Emanuel May 18 '14 at 19:52
0

I will try to answer :-

  1. As per your code you have this String ["1","Admin","123","Adm","messages","0"] which is clearly a jsonarray but you are parsing as jsonobject in android.

  2. as @Boban said you must not run network operations on main thread so avoid dat.

Now taking the response as what i mentioned in 1 here is how you will do :-

JSONArray jArray=new JSONArray(json_str)
String id=jArray.getString(0);
String admin=jArray.getString(1);
.
.
.

Hope it helps ... and if i m wrong please correct me :)

thx

Ahmad
  • 437
  • 1
  • 4
  • 12