Android 修复非法状态异常
对于上面看到的代码块,我现在得到一个非法状态异常。我最初没有收到这个错误。我需要更改API,因此请求对象中的URL不同。该错误发生在创建响应对象之后。getStockInfoFromJSON()方法未运行。我知道它没有运行,因为我在一开始就放了一个log语句,但它没有显示在控制台中。如何防止抛出此错误Android 修复非法状态异常,android,json,android-asynctask,illegalstateexception,Android,Json,Android Asynctask,Illegalstateexception,对于上面看到的代码块,我现在得到一个非法状态异常。我最初没有收到这个错误。我需要更改API,因此请求对象中的URL不同。该错误发生在创建响应对象之后。getStockInfoFromJSON()方法未运行。我知道它没有运行,因为我在一开始就放了一个log语句,但它没有显示在控制台中。如何防止抛出此错误 try { Response response = client.newCall(request).execute(); getStockInfoFromJSON(
try {
Response response = client.newCall(request).execute();
getStockInfoFromJSON(response.body().string());
} catch (IOException | JSONException | IllegalStateException e) {
Log.i("GraphAsyncTask", e.toString());
}
你是不是读了两遍回应体?只能调用
string()
一次 哪一行抛出异常?它还会告诉您是什么导致了控制台输出中的异常(在这里发布也会帮助我们)@billynomates中没有给出任何行console@billynomates我刚刚从catch块中删除了非法状态异常。当在try块中调用getStockInfroFromJson()方法时,控制台指向该方法。您是否读取响应正文两次?只能调用string()一次。
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