Skip to content Skip to sidebar Skip to footer

Volley, Json And Php Connection

I'm trying to get data from a MySql db. This is the php code :

Solution 1:

Try this:

privatestaticList<String> result = newArrayList<String>();
 publicstaticvoidmostraDati(){

    System.out.println("ww");
    JsonObjectRequest jsonObjectRequest = newJsonObjectRequest(Request.Method.POST, DatiNet.MostraDati, newResponse.Listener<JSONObject>() {
        @OverridepublicvoidonResponse(JSONObject response) {
            System.out.println("fin qui ci siamo");
            try {

                JSONArray utenti = response.getJSONArray("utenti");


                for (int i = 0; i < utenti.length(); i++) {
                    JSONObject student = utenti.getJSONObject(i);

                    String nome = student.getString("nome");
                    String cognome = student.getString("cognome");
                    String numero = student.getString("numero");
                    String email = student.getString("email");
            result.add(nome + " " + cognome + " " + email); // or use StringBuilder to create string!
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, newResponse.ErrorListener() {
        @OverridepublicvoidonErrorResponse(VolleyError error) {
            System.out.append(error.getMessage());

        }
    });   
}

to show data use this:

for(String s : result)
{
  System.out.println(s);
}

try with this php request:

<?php
mysql_connect("localhost","root","password");
mysql_select_db("DBName");
$q=mysql_query("SELECT * FROM utenti");
while($e=mysql_fetch_assoc($q))
        $output[]=$e;

print(json_encode($output));

mysql_close();
?>

Post a Comment for "Volley, Json And Php Connection"