I am trying to parse a data from the following link

Thank You Everyone for Helping out. But I found my answer from the search over the internet. Here I used VOLLEY to call the link.

JSON PARSER CLASS

 public class ParseJSON  {

        public static String[] position1;
        public static String[] team;
        public static String[] points;

        public static final String JSON_ARRAY = "data";

        public static final String CHILD_ARRAY = "standings";

        public static final String KEY_ID = "position";
        public static final String KEY_NAME = "team";

        private JSONObject users = null;
        private JSONArray user2=null;
        private JSONObject user3=null;

        private String json;

        public ParseJSON(String json){
            this.json = json;
        }
        protected void parseJSON() {
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(json);
                users = jsonObject.getJSONObject(JSON_ARRAY);
                try {
                    user2=users.getJSONArray(CHILD_ARRAY);
                    position1 = new String[user2.length()];
                    team = new String[user2.length()];
                    points=new String[user2.length()];
                    for (int i = 0; i < user2.length(); i++) {

                        JSONObject jo = user2.getJSONObject(i);
                       try {
                           user3=jo.getJSONObject("overall");
                           points[i] = user3.getString("points");
                           System.out.println("Message me: "+points[i]);
                       }catch (Exception e)
                       {
                           e.printStackTrace();
                       }
                        position1[i] = jo.getString(KEY_ID);
                        team[i] = jo.getString(KEY_NAME);
                        System.out.println("Message me: "+position1[i]);
                        System.out.println("Message me: "+team[i]);


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

Main Activity Class

 public class MainActivity extends AppCompatActivity implements View.OnClickListener {


        public static final String JSON_URL="http://soccer.sportsopendata.net/v1/leagues/premier-league/seasons/16-17/standings";
        private Button buttonGet;
        private ListView listView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            buttonGet = (Button) findViewById(R.id.buttonGet);
            buttonGet.setOnClickListener(this);
            listView = (ListView) findViewById(R.id.listView);
        }

        @Override
        public void onClick(View v) {
            sendRequest();
        }
        private void sendRequest() {
            final StringRequest stringRequest = new StringRequest(JSON_URL,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            showJSON(response);

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    });
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
        }
        private void showJSON(String json){
            ParseJSON pj = new ParseJSON(json);
            pj.parseJSON();
            CustomList cl = new CustomList(this,    ParseJSON.position1,ParseJSON.team,ParseJSON.points);
            listView.setAdapter(cl);
        }    
    }

Custom Class for adding datas to list view

public class CustomList  extends ArrayAdapter<String> {

    private String[] position1;
    private String[] team;
    private String[] points;
    private Activity context;

    public CustomList(Activity context, String[] position1, String[] team, String[] points) {
        super(context, R.layout.list_view_layout, position1);
        this.context = context;
        this.position1 = position1;
        this.team = team;
        this.points = points;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View listViewItem = inflater.inflate(R.layout.list_view_layout, null, true);
        TextView pos1 = (TextView) listViewItem.findViewById(R.id.position1);
        TextView teamname = (TextView) listViewItem.findViewById(R.id.teamname);
        TextView points1 = (TextView) listViewItem.findViewById(R.id.points);


        pos1.setText("Position: "+position1[position]);
        teamname.setText("Team: "+team[position]);
        points1.setText("Points: "+points[position]);


        return listViewItem;
    }
}

Leave a Comment