How can i get JSon data from url on Android?

2.4k Views Asked by At

the json looks like this:

[{"id":"1","name":"Mihai","email":"[email protected]","password":"1234","phone":"765889345"},{"id":"2","name":"Robin","email":"[email protected]","password":"1234","phone":"765453434"}]

the code

// Json
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectAll()
            .penaltyLog()
            .penaltyDialog()
            .build());

    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
            .penaltyLog()
            .build());
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectDiskReads()
            .detectDiskWrites()
            .detectNetwork() // or .detectAll() for all detectable problems
            .penaltyLog()
            .build());
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
            .detectLeakedSqlLiteObjects()
            .penaltyLog()
            .penaltyDeath()
            .build());


    TextView uid = (TextView) findViewById(R.id.titlu_anunt1);
    TextView name1 = (TextView) findViewById(R.id.descriere_anunt1);
    TextView email1 = (TextView) findViewById(R.id.telefon_anunt1);


    JSONObject json = null;
    String str = "";
    HttpResponse response;
    HttpClient myClient = new DefaultHttpClient();
    HttpPost myConnection = new HttpPost("http://appz.esy.es/get_user.php");

    try {
        response = myClient.execute(myConnection);
        str = EntityUtils.toString(response.getEntity(), "UTF-8");

    }
    catch (ClientProtocolException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }


    try {
        JSONArray jArray = new JSONArray(str);
        json = jArray.getJSONObject(0);


        uid.setText(json.getString("id"));
        name1.setText(json.getString("name"));
        email1.setText(json.getString("email"));


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

How can I add all the objects to my textviews? Right now I can only display one object from json in my first textview, I want to increment the id. I want to display the id1 information: name, location and other stuff in textviews 1. After that I want to display for id2 the name and location into textviews 2.

1

There are 1 best solutions below

4
On BEST ANSWER

You can use the volley library, it's easy to use and you can focus on your app rather than on how getting your data. You can add the library to your project following this tutorial or if you are using AndroidStudio you can add it to your gradle as seen here.

To make a request you just need to add this:

        //I think a StringRequest is more convenient in your case cause you can 
        //make a new JSONArray straight from the String
    StringRequest request = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //Here is where you process your data, you could call a method to parse your data 
            parseData(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //Here is where you handle the errors
        }
    });

    RequestQueue queue = Volley.newRequestQueue(Here_goes_a_Context_object);
    //This will send the request
    queue.add(request);

    public void parseData(String response) {

          try {

              //Your JSONArray doesn't have a name so you can't find it with the USER_TAG
              //You need to make your JSONObject a String and then make a new JSONArray with that String
              user = new JSONArray(response);

              //The rest of the code looks good to me
              ...
          }
      }