使用Alpha Vantage解析JSON数据Android Studio时出错

时间:2017-11-03 04:07:45

标签: android json api alphavantage

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

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

3 个答案:

答案 0 :(得分:1)

问题在于这一行String addThis = jsonObject.getJSONObject("20171102").getString("close");

JSON中,您没有20171102的密钥。

因为您的JSON有很多JSONObject。他们有钥匙。

因此,您可以使用Iterator<String> iterator = time.keys();来解析密钥。

试试这个。

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

答案 1 :(得分: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();
    }
}

答案 2 :(得分:0)

我创建了一个完整的库存清单示例,使用可能有用的新Android语言Kotlin和Alpha-Vantage。它提取各种股票的收盘价,并将其张贴在屏幕上的列表中。

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