-2

I have honestly tried so many tutorials and have come up short. Can someone please guide me as to how I can take a simple JSON array from a url and then populate a listview in Android studio. Most the tutorials I have tried are outdated and deprecated. Please any help would be truly appreciated.

The Json Array from url:

[
 { 
  "lot":"A", 
  "spaces":"100",
  "rates":"7.00"
 },
 {
  "lot":"B",
  "spaces":"207",
  "rates":"5.00"
 }
]

I have tried volley, httpcall and various other methods. Is there a simple guide anywhere that i can follow?

Bundula
  • 1
  • 4
  • Read this answer: http://stackoverflow.com/questions/13196234/simple-parse-json-from-url-on-android – Musa Y. Oct 26 '15 at 03:06
  • Yes I came across this during my extensive search to find a tutorial. But unfortunately, I am not able to fully grasp the answer posted. I was hoping for a complete tutorial. Hope I am not asking for much – Bundula Oct 26 '15 at 03:19

3 Answers3

2

You can use following code

 public class MyActivity extends Activity {
    ListView list;
    ArrayList<Data> myList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

      list = (ListView) findViewById(R.id.list);
      myList = new ArrayList<>();
      LoadData sl=new LoadData();
                        sl.execute();
  }
}
        class LoadData extends AsyncTask<String, String, String>{
            ProgressDialog pd;
            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                pd=new ProgressDialog(Login.this);
                pd.setCancelable(false);
                pd.setMessage(getString(R.string.loading_login));
                pd.show();
            }
            @Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub
                String buffervalue="";


                try{
    //              StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    //              StrictMode.setThreadPolicy(policy); 
                  DefaultHttpClient httpclient = new DefaultHttpClient();

                  HttpPost httpget = new HttpPost("url");
                  HttpResponse response = httpclient.execute(httpget);

                  InputStream inputStream = response.getEntity().getContent();

                  byte[] data = new byte[256];

                  StringBuffer buffer = new StringBuffer();
                  int len = 0;
                  while (-1 != (len = inputStream.read(data)) )
                  {
                      buffer.append(new String(data, 0, len));
                  }
                  inputStream.close();
                  buffervalue=buffer.toString();
                }catch(Exception e){

                }

                  return buffervalue;

            }
            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                JSONObject jsonobject;
                try {
                    JSONArray jarr = new JSONArray(result);
                    int lengthJsonArr = jarr.length();  
                    if(lengthJsonArr>0)
                    {   
                        for(int i=0;i<lengthJsonArr;i++){
                        JsonObject obj = jarr.get(i);
                        Data data = new Data();
                        data.setLot(obj.getString("lot"));  
                        data.setSpaces(obj.getString("spaces"));
                        data.setRates(obj.getString("rates"));

                       myList.add(data)
                    }
                    adapter.notifyDataSetChanged();
                        }else{
                            Toast.makeText(getApplicationContext(),R.string.label_internet, Toast.LENGTH_SHORT).show();
                        }
                    }
                }catch(Exception e){
                    Toast.makeText(getApplicationContext(),"Something wrong with url...", Toast.LENGTH_SHORT).show();
                }
                pd.dismiss();
            }
        }
H Raval
  • 1,903
  • 17
  • 39
  • Is this a new class or do I put this in my MainActivity.java class? Also I'm trying to understand the logic behind this code. Basically with the little knowledge I have, I see that you are AsyncTask to read the JSON Array from the url. But then what are you doing with it? Why is there an option to enter a username/password? Once again thank you for your responses :) – Bundula Oct 26 '15 at 05:42
  • You need to create a custom adapter class and set it to listView and add your all data in one arraylist. After then you need to pass that arraylist to your custom adapter class constructor. – Piyush Oct 26 '15 at 05:44
  • put this class in your main activity as private class and execute it....in this example tost is just to inform data is not correct,you can omit it if you dont need – H Raval Oct 26 '15 at 06:05
  • @HetalUpadhyay Please modify your answer for populate data in listView – Piyush Oct 26 '15 at 06:07
  • see edit, create your adapter for listview and set it to listview, and also create one model class named Data for your json data – H Raval Oct 26 '15 at 06:17
  • yes, i am also form abad – H Raval Oct 26 '15 at 06:18
  • Wow. That's good. Same here. Hu pan ! How much experience do you have in android ? @HetalUpadhyay – Piyush Oct 26 '15 at 06:18
  • can v just focus on your question? get it worked first then chit chat – H Raval Oct 26 '15 at 06:26
  • @HetalUpadhyay Its not mine question. The OP is different ! – Piyush Oct 26 '15 at 06:29
  • @HetalUpadhyay Your code will be worked but some of methods and data type has been deprecated. – Piyush Oct 26 '15 at 06:42
  • why dont you provide any useful code? – H Raval Oct 26 '15 at 07:25
  • Because the question is primilary based and off topic. But your answer seems ok. – Piyush Oct 26 '15 at 07:44
  • @HetalUpadhyay Now the answer has been upvoted by me. Now can you tell about ur experience in android? – Piyush Oct 26 '15 at 08:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93340/discussion-between-hetal-upadhyay-and-piyush-gupta). – H Raval Oct 26 '15 at 08:57
0

Use this technique such simple and easy technique for json parsing in android.

saveen-android.blogspot.in/2014/10/json-parsing.html?m=1

Neeraj Sharma
  • 191
  • 18
  • Thank you, but once again this tutorial is rendered useless because the org.apache.http.clients are deprecated and are not supported according to my research – Bundula Oct 26 '15 at 03:42
0

Hopefully the following links will help you as you can download the source code:

  1. Android listview: load data from json
  2. How to populate ListView with remote JSON data
  3. Android Custom ListView with Image and Text using Volley
Musa Y.
  • 1,747
  • 18
  • 26