0

您好,我在让 Alpha Vantage api 按我想要的方式工作时遇到问题。我只想得到最后几天的收盘价。我不确定我做错了什么。我唯一的目标是在最后几天关闭时简单地更改为 TextView“tvStockClose”。现在的错误是运行时错误。任何方向的任何帮助都将受到赞赏和欢迎。

API:链接

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();
        }
4

3 回答 3

1

你的问题是这条线String addThis = jsonObject.getJSONObject("20171102").getString("close");

在你的JSON,你没有钥匙20171102

因为你JSON有很多JSONObject。而且他们有关键。

所以你可以Iterator<String> iterator = time.keys();用来解析 key 。

尝试这个 。

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();
    }
}
于 2017-11-03T04:28:46.117 回答
0

我创建了一个完整的股票列表示例,使用新的 Android 语言 Kotlin 和 Alpha-Vantage 可能会有所帮助。它提取各种股票的收盘价并将其发布在屏幕上的列表中。

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

于 2017-11-29T04:16:33.193 回答
0

您需要先将最后一天的日期作为字符串获取,然后在该键上获取 JSON 对象。

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();
    }
}
于 2017-11-03T04:49:29.653 回答