OkHttp同步非同步請求
阿新 • • 發佈:2019-02-15
OkHttpClient okHttpClient = new OkHttpClient(); FormBody.Builder builder = new FormBody.Builder(); builder.add("key","value"); FormBody body = builder.build(); Request request =new Request.Builder().url("").post(body).build(); Call call = okHttpClient.newCall(request); // 非同步請求 call.enqueue(newCallback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String json = response.body().string(); } }); // 同步請求 // try { // Response response = call.execute();// String json = response.body().string(); // } catch (IOException e) { // e.printStackTrace(); // }