zoukankan      html  css  js  c++  java
  • Retrofit框架的使用

    一、Get请求

    private void Retrofit_Get() {
    Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(Urls.baseUrl)
    .build();
    MyServerInterface myServerInterface = retrofit.create(MyServerInterface.class);
    Call<ResponseBody> call = myServerInterface.getLastJsonString();
    call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
    if (response.isSuccessful()) {
    String json = null;
    try {
    json = response.body().string();
    mTextView.setText(json);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
    Log.d(TAG, "onFailure() called with: " + "call = [" + call + "], t = [" + t + "]");
    }
    });
    }

    服务接口可以这样写:
    public interface MyServerInterface {
    @GET("341-2?maxResult=20&page=&showapi_appid=19170&showapi_sign=248b52a91c9d4d2fa5ca1ddd16ee7832")
    Call<ResponseBody> getLastJsonString();

    @GET("341-2?maxResult=20&page=&showapi_appid=19170&showapi_sign=248b52a91c9d4d2fa5ca1ddd16ee7832")
    Call<JokeModel> getJson2Model();
    }


















  • 相关阅读:
    复利计算(1)
    对IT行业的一些思考
    递归下降语义分析
    1118 实验三 有限自动机的构造与识别
    冒泡排序文法评论
    实验0:了解和熟悉操作系统
    0302思考IT行业的感想
    递归下降语义分析
    对文法解释和语法树的评论
    语言文法
  • 原文地址:https://www.cnblogs.com/GeChuangGuang/p/5508482.html
Copyright © 2011-2022 走看看