1. 程式人生 > >OkHttp同步非同步請求

OkHttp同步非同步請求

 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(new 
Callback() { @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(); // }