try {
Response response = client.newCall(request).execute();
getStockInfoFromJSON(response.body().string());
} catch (IOException | JSONException | IllegalStateException e) {
Log.i("GraphAsyncTask", e.toString());
}
For the code block seen above, I am now getting an IllegalStateException. I wasn't getting this error originally. I needed to change the API, so the URL in the request object is different. The error comes after the Response object is created. The getStockInfoFromJSON() method isn't running. I know it isn't running because I placed a log statement at the very beginning and it didn't show up in the console. How can I keep this error from being thrown.
private void getStockInfoFromJSON(String JsonString)
throws JSONException {
Log.i("MSA JSON string", JsonString);
MyStocksActivity.StockData = new ArrayList<>();
JSONObject Json = new JSONObject(JsonString);
JSONObject StockInfo = Json.getJSONObject("Time Series (Daily)");
JSONArray array = new JSONArray();
StockInfo.toJSONArray(array);
Log.i("MSA", "JSON array " + StockInfo.toString());
for (int i = 0; i < array.length(); i++) {
StockData datum = new StockData();
JSONObject stockPrice = array.getJSONObject(i);
Log.i("stock price array", stockPrice.toString());
String date = array.getString(i);
Log.i("date", date);
String[] stringDate = date.split("-");
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(stringDate[0]),
Integer.parseInt(stringDate[1]), Integer.parseInt(stringDate[2].substring(0, 2)));
Log.i("cal", cal.toString());
datum.date = cal.getTimeInMillis();
datum.price = array.getDouble(3);
datum.CalDate = date.split(" ")[0];
MyStocksActivity.StockData.add(datum);
}
}
This is the error message I get.
07-03 15:05:29.586 27929-28167/com.example.sam_chordas.stockhawk I/GraphAsyncTask: java.lang.IllegalStateException: closed
Are you reading the response body twice? You can only call string()
once.