Hi JsonArray Help

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Sep 6, 2017 5:18:33 PM via Website

i am stuck in the return i wish to know how to show this dates like position, points, wins, losses, but android say getCurrentDetails but didin't work... how this need to return.

Thank you Pedro Duarte

    String soccerURL = " --------------------------------------------------------"

    if (isNetworkAvailable()) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(soccerURL)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                try {
                    String jsonData = response.body().string();
                    Log.v(TAG, jsonData);
                    if (response.isSuccessful()) {
                        mTableSoccer = getCurrentDetails(jsonData);
                    } else {
                        alertUserAboutError();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Exception caught: ", e);
                } catch (JSONException e) {
                    Log.e(TAG, "Exception caught: ", e);
                }

            }
        });

    } else {
        Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG).show();
    }

    Log.d(TAG, "Main UI code is running!");

}


private TableSoccer getCurrentDetails(String jsonData) throws JSONException {

    JSONObject table = new JSONObject(jsonData);
    String leagueCaption = table.getString("leagueCaption");
    Log.i(TAG, "From Json: " + leagueCaption);
    JSONArray standingArray = table.getJSONArray("standing");

    //this will be used to hold all the extracted objects inside standingArray.
    ArrayList<TableSoccer> tableSoccerElements = new ArrayList<>();

    for (int i = 0; i < standingArray.length(); i++) {

        JSONObject standingObject = standingArray.getJSONObject(i);

        TableSoccer tableSoccer = new TableSoccer();
        tableSoccer.setmPosition(standingObject.getInt("position"));
        tableSoccer.setmPoints(standingObject.getInt("points"));
        tableSoccer.setmWins(standingObject.getInt("wins"));
        tableSoccer.setmLosses(standingObject.getInt("losses"));



        tableSoccerElements.add(tableSoccer);
    }

    return ;
}