OkHttpGson
Tue Apr 05 2022 11:13:22 GMT+0000 (Coordinated Universal Time)
Saved by
@oliversinz
import com.google.gson.Gson;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;
public class OkHttpGson {
public static void main(String[] args) throws Exception {
// avoid creating several instances, should be singleton
OkHttpClient client = new OkHttpClient();
// creating a request object
Request request = new Request.Builder()
.url("https://api.quotable.io/random")
.build();
// Decode the ResponseBody with Gson
Gson gson = new Gson();
ResponseBody responseBody = client.newCall(request).execute().body();
Quote entity = gson.fromJson(responseBody.string(), Quote.class);
System.out.println(entity.getAuthor());
System.out.println(entity.getContent());
}
}
content_copyCOPY
Comments