private RetrofitService service; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.e("测试retrofit", "onCreate----"); setContentView(R.layout.load_more_view); Retrofit retrofit = new Retrofit.Builder() //创建Retrofit实例 .baseUrl("https://api.apiopen.top/") //url头部 .addConverterFactory(GsonConverterFactory.create()) //返回的数据经过转换工厂转换成我们想要的数据,最常用的就是Gson .build(); //构建实例 service = retrofit.create(RetrofitService.class); //使用retrofit的create()方法实现请求接口类 request(); }
//请求古诗的方法 private void request() { Call<Poetry> call = service.getPoetry("静夜思"); //通过getPoetry()方法获取Call对象 call.enqueue(new Callback<Poetry>() { //call入列发送请求,传入回调Callback,重写成功onRespons()与失败onFailure()方法,并编写相应逻辑 /** * 请求成功 * */ @Override public void onResponse(Call<Poetry> call, Response<Poetry> response) { //返回的response中包含了所有信息,其中response.body就是响应主体 Log.e("Retrofit测试", "--------onSuccess-----------" + response.body().getResult().get(0).getTitle()); Log.e("Retrofit测试", "--------onSuccess-----------" + response.body().getResult().get(0).getAuthors()); Log.e("Retrofit测试", "--------onSuccess-----------" + response.body().getResult().get(0).getContent().replace("|", "\n")); if (response.body().getResult().size() > 0) { //由于我们已经添加了Gson转换工厂,所以响应主体会被转换成Poetry对象,使用对象响应方法判断返回的古诗是否大于一首 // title.setText(response.body().getResult().get(0).getTitle()); //数据展示 // author.setText(response.body().getResult().get(0).getAuthors()); // String contentText = response.body().getResult().get(0).getContent().replace("|", "\n"); //由于接口返回的古诗体用"|"代替换行,所以我们想要换行输出就要替换回来 // content.setText(contentText); } else { Toast.makeText(NetActivity.this, "这就触及到我的知识盲区了", Toast.LENGTH_SHORT).show(); //古诗没有大于一首 } } /** * 请求失败 * */ @Override public void onFailure(Call<Poetry> call, Throwable t) { Log.d("ManiActivity", t.getMessage()); //控制台打印报错原因 Log.e("Retrofit测试", "--------onFail-----------" + t.getMessage()); } }); } package com.union_test.toutiao.RetrofitAbout; import java.util.List; /** * Created by DaYu on 2019/5/23. * Describe: */ public class Poetry { private int error_code; private String reason; /** * code : 200 * message : 成功! * result : [{"title":"静夜思","content":"床前看月光,疑是地上霜。|举头望山月,低头思故乡。","authors":"李白"}] */ private int code; private String message; private List<ResultBean> result; public int getError_code() { return error_code; } public void setError_code(int error_code) { this.error_code = error_code; } public String getReason() { return reason; } public void setReason(String reason) { this.reason = reason; } @Override public String toString() { return "Poetry{" + "error_code=" + error_code + ", reason='" + reason + '\'' + '}'; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public List<ResultBean> getResult() { return result; } public void setResult(List<ResultBean> result) { this.result = result; } public static class ResultBean { /** * title : 静夜思 * content : 床前看月光,疑是地上霜。|举头望山月,低头思故乡。 * authors : 李白 */ private String title; private String content; private String authors; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getAuthors() { return authors; } public void setAuthors(String authors) { this.authors = authors; } } }
/** * Created by DaYu on 2019/5/23. * Describe: */ public interface RetrofitService { //get请求接口 @GET("searchPoetry") //指定请求类型的注解 Call<Poetry> getPoetry(@Query("name") String name); //获取古诗的方法
最后不要忘了网络权限申请