-1
try {
Response response = client.newCall(request).execute();
getStockInfoFromJSON(response.body().string());
} catch (IOException | JSONException | IllegalStateException e) {
Log.i("GraphAsyncTask", e.toString());
}
對於上面看到的代碼塊,我現在得到一個IllegalStateException。我原來並沒有得到這個錯誤。我需要更改API,因此請求對象中的URL是不同的。該錯誤出現在創建Response對象之後。 getStockInfoFromJSON()方法未運行。我知道它沒有運行,因爲我在一開始就放置了一條日誌語句,並沒有顯示在控制檯中。我怎樣才能避免這個錯誤被拋出。修復IllegalStateException
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);
}
}
這是我得到的錯誤消息。
07-03 15:05:29.586 27929-28167/com.example.sam_chordas.stockhawk I/GraphAsyncTask: java.lang.IllegalStateException: closed
哪一行會引發異常? –
此外,它會告訴你是什麼導致在控制檯輸出異常(張貼在這裏也會幫助我們) –
@billynomates在控制檯中沒有行 –