0

MainClass.java

RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    RecyclerView.Adapter adapter;
    private Toolbar mToolbar;
    private List<Onwardflights> onwardflights;
    private static final String url = "http://tripofareapi.herokuapp.com/flightapi/src=DEL&dest=BOM&depdate=20180420&arrdate=20180422"; // https: url will  insert here
    ProgressDialog progressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flight_suggestion);
        mToolbar = (Toolbar) findViewById(R.id.flight_suggestion_appbar);

        Bundle bundle = getIntent().getExtras();
        String from = bundle.getString("from");
        String to = bundle.getString("to");

        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle(from + "-" + to);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mToolbar.setTitleTextColor(Color.WHITE);

        recyclerView = (RecyclerView) findViewById(R.id.flight_suggestion_recyclerview);
        recyclerView.setHasFixedSize(true);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        onwardflights = new ArrayList<>();
        getdata();


    }

This is what i have done

    private void getdata() {

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please wait, while we load the data");
        progressDialog.show();

       //*Could not understand how to parse FARE objects*
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                progressDialog.dismiss();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jo = jsonArray.getJSONObject(i);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(request);
    }
2
  • provide your json data sample. Commented Apr 15, 2018 at 6:09
  • the url is present in the code Commented Apr 15, 2018 at 6:10

2 Answers 2

1

Try this.

 private void getdata() {

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please wait, while we load the data");
        progressDialog.show();

       //*Could not understand how to parse FARE objects*
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                progressDialog.dismiss();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONObject data = jsonObject.getJSONObject("data");
                    JSONArray  returnflights = data.getJSONArray("returnflights");
                 JSONArray  onwardflights = data.getJSONArray("onwardflights");
                    for (int i = 0; i < returnflights.length(); i++) {
                        JSONObject returnFlightChild = returnflights.getJSONObject(i);
                     //returnFlights objects
                    }
                   for (int i = 0; i < onwardflights.length(); i++) {
                        JSONObject onwardFlightsChild = onwardflights.getJSONObject(i);
                   //onwardFlight objects
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(request);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

First check your response format in Json formatter and validator.

Your json response has 2 json object

  1. data
  2. data_length

to get JSONObject form a Json user getJSONObject("key") and to get JSONArray from Json use getJSONArray("key").

As node data is a JSONObject use getJSONObject("key")` to get that.

    JSONObject jsonObject = new JSONObject(response);
    JSONObject data = jsonObject.getJSONObject("data");

    JSONArray  jsArray1 = data.getJSONArray("returnflights");
    JSONArray  jsArray2 = data.getJSONArray("onwardflights");

    for (int i = 0; i < jsArray1.length(); i++) {
        JSONObject jo = jsArray1.getJSONObject(i);

    }

    ..................

Check this tutorial to learn how to parse Json

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.