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:

 private static List<String> result = new ArrayList<String>();
 public static void mostraDati(){

    System.out.println("ww");
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, DatiNet.MostraDati, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(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();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(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"