發現
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(DATA_URL)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG, "onFailure: " + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d(TAG, "onResponse: " + response.body().string());
String result = response.body().string();
parseJson(result);
}
});
這樣寫居然會報
java.lang.IllegalStateException: closed
此exception,
後來發現
原來
response.body().string()
被調用一次就會被close了,
只要將 Log.d註解起來,直接存入String再使用即可通過編譯
沒有留言:
張貼留言