retrofit展示数据

接口

package com.example.retrofit.connector;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;

/**
 * date:2017/5/21
 * author:易宸锋(dell)
 * function:
 */

public interface MyServerInterface {
    ///
    // GET网络请求方式
    ///
    /**Get请求,方法中无参数
     * 作用:GET请求最简单的写法,无Path参数和Query参数
     * article/list/latest?page=%d实际是Constant下的URL_LATEST地址
     * @GET()里的东西是要拼接的网址,注意直接把page=1了
     */
    @GET("article/list/latest?page=1")
    Call<ResponseBody> getLatestJsonString();
}
 

接口

package com.example.retrofit.net;

/**
 * 项目糗事百科的网址
 */
public class Constant {
    //baseurl
    public final static String URL_BASE = "http://m2.qiushibaike.com/";
}
 

主页面

package com.example.retrofit;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;


import com.example.retrofit.connector.MyServerInterface;
import com.example.retrofit.net.Constant;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

//Retrofit之基本使用,get请求无参数模式,注意添加网络权限
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private TextView textView_info;
    private Call<ResponseBody> mCall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        textView_info = (TextView) findViewById(R.id.textView_info);

        //初始化Retrofit
        initRetrofit();
    }

    private MyServerInterface serverInterface = null;

    private void initRetrofit() {
        //创建 retrofit 对象
        /*Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://m2.qiushibaike.com/")
                .build();*/

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.URL_BASE)
                .build();

        //创建接口对象,参数:接口的字节码
        serverInterface = retrofit.create(MyServerInterface.class);

        //类似okhttp,通过接口对象,调用抽象方法,创建出call对象
        mCall = serverInterface.getLatestJsonString();
        mCall.enqueue(new Callback<ResponseBody>() {
            //请求成功的回调(Retrofit与okhttp不同,回调方法是运行在主线程而不是子线程的)
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                //打一个log
                Log.d(TAG, "---->threadId:" + Thread.currentThread().getId());
                //对返回结果做成功与内容的判断
                if (response.isSuccess() && response.body() != null) {
                    try {
                        //得到结果
                        String result = response.body().string();
                        //更新UI
                        textView_info.setText(result);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            //请求失败的回调
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {}
        });
    }

    //为节约资源,当屏幕不可见时,我们停止网络请求
    @Override
    protected void onStop() {
        super.onStop();
        if (mCall != null) {
            mCall.cancel();
        }
    }
}
//Retrofit2的依赖
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'

权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
 

布局默认的就行,不要忘了加id


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Retrofit是一个用于在Android应用中进行网络请求的库。它通过简化网络请求的过程,提供了一种方便的方式来与后台服务器进行通信。 在Retrofit中,首先需要创建一个RetrofitService类。这个类负责配置Retrofit的基础URL和创建Retrofit实例。可以通过调用RetrofitService的create方法来创建具体的服务接口实例。这个服务接口定义了与后台服务器进行通信的方法。 在使用Retrofit时,可以通过Converter来将服务器返回的数据转换成Java对象。Retrofit支持多种转换器,可以根据数据的格式选择合适的转换器。 通过调用Retrofit的execute方法或enqueue方法可以发送网络请求。execute方法将请求发送到后台并即时返回结果,而enqueue方法将请求放入请求队列中,并在请求完成后通过Callback回调返回结果。 综上所述,Android中使用Retrofit进行网络请求的步骤包括:配置Retrofit的基础URL、创建服务接口实例、定义请求方法、选择合适的转换器、发送网络请求并处理响应。 引用: : 这段代码展示了如何在Android中使用Retrofit进行网络请求。 : 这段代码展示了创建Retrofit实例和服务接口实例的方式。 : 这段代码展示了最初使用Retrofit的方式,其中包括设置基础URL和创建Retrofit实例的步骤。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [android retrofit 从无知到入门](https://blog.csdn.net/shop_and_sleep/article/details/123526236)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值