-1

the json looks like this:

[{"id":"1","name":"Mihai","email":"mihai@yahoo.com","password":"1234","phone":"765889345"},{"id":"2","name":"Robin","email":"robin@yahoo.com","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.

Kara
  • 6,115
  • 16
  • 50
  • 57
dodo
  • 38
  • 12
  • 1
    What's your problem? YOu can't fetch the json? You can't parse the json? or you can't use the parsed json to update your app's views? – Marc B Nov 28 '14 at 15:05
  • `json = sb.toString();`. Ok. Now json should contain your mentioned json text. Now is it ok so far? – greenapps Nov 28 '14 at 15:07
  • Try to user Android Volley for such request - It has JSONObjectRequest. – Robert Nov 28 '14 at 15:08
  • doesnt start the activity where i should have the textviews with data from that url – dodo Nov 28 '14 at 15:09
  • Can anyone help me with my question ? [Getting duration from Json Data][1] [1]: https://stackoverflow.com/questions/28364705/getting-duration-using-json-parsing-from-google-map – Jinal Feb 06 '15 at 14:59

1 Answers1

0

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
              ...
          }
      }
Carlos J
  • 2,965
  • 4
  • 17
  • 28
  • try { user = new JSONArray(json.toString()); JSONObject c = user.getJSONObject(0); // Storing JSON item in a Variable String id = c.getString(TAG_ID); String name = c.getString(TAG_NAME); String email = c.getString(TAG_EMAIL); you mean like this ? but nothing happened – dodo Nov 28 '14 at 15:31
  • It really isn't that complicated. What's exactly is the problem with your program? Can you get the JSONObject with the method you already had? – Carlos J Nov 28 '14 at 16:02
  • i got an app... and i want to get some informations about some announcements...name, location , phone.... i made the json on this website here http://appz.esy.es/get_anunturi.php ...is an array of objects ..of what ive learned... and i want to get those informations to show them into textviews on my app... and doesnt work, i tryed to start that activity but doesnt start...and i dont know why... maybe because my json is an array doesnt have name...or maybe the url...or textviews..i got no ideea – dodo Nov 28 '14 at 16:04
  • What error is the LogCat showing? without that information it's impossible to know where the problem is – Carlos J Nov 28 '14 at 18:19