2017-07-03 5 views
-1
try { 
     Response response = client.newCall(request).execute(); 
     getStockInfoFromJSON(response.body().string()); 
    } catch (IOException | JSONException | IllegalStateException e) { 
     Log.i("GraphAsyncTask", e.toString()); 
    } 

Für den Codeblock, den ich oben gesehen habe, erhalte ich jetzt eine IllegalStateException. Ich habe diesen Fehler ursprünglich nicht bekommen. Ich musste die API ändern, daher ist die URL im Anfrageobjekt anders. Der Fehler tritt auf, nachdem das Response-Objekt erstellt wurde. Die Methode getStockInfoFromJSON() wird nicht ausgeführt. Ich weiß, dass es nicht läuft, weil ich eine Log-Anweisung ganz am Anfang platziert habe und sie nicht in der Konsole angezeigt wurde. Wie kann ich verhindern, dass dieser Fehler ausgelöst wird?Fehlerbehebung 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); 

    } 
} 

Dies ist die Fehlermeldung, die ich bekomme.

07-03 15:05:29.586 27929-28167/com.example.sam_chordas.stockhawk I/GraphAsyncTask: java.lang.IllegalStateException: closed 
+0

Welche Zeile löst die Ausnahme aus? –

+0

Auch es wird Ihnen sagen, was verursacht die Ausnahme in der Konsole Ausgabe (es hier zu veröffentlichen wird uns auch helfen) –

+0

@billynomates keine Zeile wurde in der Konsole gegeben –

Antwort

1

Liest du den Antwortkörper zweimal? Sie können string() nur einmal aufrufen.