【发布时间】:2017-07-03 19:43:07
【问题描述】:
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() 方法未运行。我知道它没有运行,因为我在一开始就放置了一个日志语句并且它没有出现在控制台中。我怎样才能防止这个错误被抛出。
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 控制台中没有给出任何行
-
@billynomates 我刚刚从 catch 块中删除了非法状态异常。在 try 块中调用时,控制台指向 getStockInfroFromJson() 方法
-
您是否阅读了两次响应正文?你只能调用 string() 一次。
标签: android json android-asynctask illegalstateexception