0

Hello I am having issues getting Alpha Vantage api to work how I want. All I want to get is the last days close price. I am not exactly sure what I am doing wrong. A the moment my only goal is to simply change to TextView "tvStockClose" the last days close. The error right now is a run time error. Any help in any direction is appreciated and welcomed.

API: link

public void loadData() {
             progressDialog.setMessage("Retrieving Data, Please Be Patient");
            progressDialog.show();
            Toast.makeText(getApplicationContext(), "1 :D", Toast.LENGTH_SHORT).show();


            StringRequest stringRequest = new StringRequest(Request.Method.GET,
                    URL2,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {

                             Toast.makeText(getApplicationContext(), "2 :D", Toast.LENGTH_SHORT).show();
                            try {
                                Toast.makeText(getApplicationContext(), "3:D", Toast.LENGTH_SHORT).show();
                                mStockList.clear();
                                JSONObject jsonObject = new JSONObject(response);


                                String addThis = jsonObject.getJSONObject("20171102").getString("close");

                                tvStockClose.setText(addThis);

                                TestStockList testStockList = new TestStockList(addThis);
                                mStockList.add(testStockList);

                                Toast.makeText(StockTest.this, addThis, Toast.LENGTH_SHORT).show();



                                mAdapter = new TestMyAdapterStockList(mStockList, getApplicationContext());

                                recyclerView.setAdapter(mAdapter);
                                progressDialog.dismiss();

                                Toast.makeText(getApplicationContext(), "4 :D", Toast.LENGTH_SHORT).show();



                            } catch (JSONException e) {

                                //Toast.makeText(getContext(), "5 :D", Toast.LENGTH_SHORT).show();
                                e.printStackTrace();
                            }

                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                    Toast.makeText(getApplicationContext(), "6 :D", Toast.LENGTH_SHORT).show();
                    Toast.makeText(getApplicationContext(), "Something Went Wrong, try Again", Toast.LENGTH_SHORT).show();
                }
            });

            RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
            requestQueue.add(stringRequest);
            Toast.makeText(getApplicationContext(), "7 :D", Toast.LENGTH_SHORT).show();
        }
jww
  • 97,681
  • 90
  • 411
  • 885
Stephan
  • 1
  • 3

3 Answers3

1

You problem is this line String addThis = jsonObject.getJSONObject("20171102").getString("close"); .

In your JSON ,you don't have the key of 20171102 .

Because your JSONhas a lot of JSONObject .And they has key .

So you can use Iterator<String> iterator = time.keys(); to parse key .

try this .

private void parseData(String response) {
    try {
        // your response
        JSONObject jsonObject = new JSONObject(response);
        // get Time
        JSONObject time = jsonObject.getJSONObject("Time Series (Daily)");
        Iterator<String> iterator = time.keys();
        while (iterator.hasNext()) {
            // get date
            String date = iterator.next().toString();
            // get jsonobject by date tag
            JSONObject dateJson = time.getJSONObject(date);
            // get string
            String close = dateJson.getString("4. close");
            Log.d("data", "4. close=" + close);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
0

You need to get last day's date as string first and then get JSON object on that key.

private Date yesterday() {
    final Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, -1);
    return cal.getTime();
}

private String getYesterdayDateString() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(yesterday());
}

private void parseData(String response) {
    try {
        JSONObject responseJson = new JSONObject(response);

        JSONObject timeSeriesJson = responseJson.getJSONObject("Time Series (Daily)");

        JSONObject dailyObject = timeSeriesJson.getJSONObject(getYesterdayDateString());

        String closePrice = dailyObject.getString("4. close");

        Log.d("closePrice", closePrice);

    } catch (JSONException e) {
        e.printStackTrace();
    }
}
adnanyousafch
  • 1,172
  • 9
  • 26
  • Hey, thanks for the help, When I try this I get the close price as null. I thought initially the issue was when calling the date methods, the api does not have those dates, but it still returns null. – Stephan Nov 06 '17 at 05:59
  • I would recommend using debug points to see what exactly are you getting in each object. https://developer.android.com/studio/debug/index.html – adnanyousafch Nov 06 '17 at 08:21
0

I have created a full stock list example of using the new Android language Kotlin with Alpha-Vantage that might help. It extracts the closing price of various stocks and posts it in a list on the screen.

http://www.todroid.com/creating-a-stock-pricing-application-with-kotlin-for-android/

mike gold
  • 1,551
  • 12
  • 12
  • HI Mike can you post the link to the full android project. Would like to use as a reference. Thanks in advance – AnilV Jun 21 '18 at 16:00