Android框架:Retrofit2之第一集简单应用

最近发现网络请求框架Retrofit应用的特别广泛,好多人都在用,特地抽空研究了一下,把我的一些学习心得写下来,最简单的应用,以后学习到更多再回来继续更新,不对的地方请大神们指正,一定虚心认真听取。

1.Retrofit是什么东西:Retrofit是一个网络请求的第三方框架,据说性能好,速度快,而且简单方便(我学过的第三方框架都很简单,我感觉不简单的话就不叫第三方框架了)

默认使用Gson解析,最大的特点是运用注解和动态代理

2.使用步骤

1:添加依赖

compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'

2:写一个接口


3:实例化Retrofit


4:通过Retrofit实例创建接口服务的对象


5:接口服务对象,调用接口中的方法,获得call对象


6:Call对象执行请求


开始撸代码


1.添加依赖


2,写一个接口

package com.retrofitdemo;

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

public interface MyServerInterface {
    @GET("/mobile/v1/album?albumId=2839012")
    Call<ResponseBody> getLatesJsonString();
}


3.实例化Retrofit


Retrofit retrofit = new Retrofit.Builder()
                //也可以不要baseUrl这个方法,用于拼接接口里面的网址
                .baseUrl("http://mobile.ximalaya.com")
                .build();

4,通过Retrofit实例创建接口服务对象

MyServerInterface serverInterface = retrofit.create(MyServerInterface.class);

5:接口服务对象,调用接口中的方法,获得call对象


Call<ResponseBody> call = serverInterface.getLatestJsonString();


6.Call对象执行请求

call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccess()) {
                    try {
                        String result = response.body().string();//拿到josn数据
                        textView.setText(result);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });

以下是全部代码

package duanlian.com.retrofigdemo;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.io.IOException;

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

public class MainActivity extends AppCompatActivity {
    private Context context = this;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initRetrofig();
    }

    /**
     * 用来初始化retrofig
     */
    private void initRetrofig() {
        Retrofit retrofit = new Retrofit.Builder()
                //也可以不要baseUrl这个方法,用于拼接接口里面的网址
                .baseUrl("http://mobile.ximalaya.com")
                .build();
        MyServerInterface serverInterface = retrofit.create(MyServerInterface.class);
        Call<ResponseBody> call = serverInterface.getLatestJsonString();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccess()) {
                    try {
                        String result = response.body().string();
                        textView.setText(result);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }

    /**
     * 用来初始化控件
     */
    private void initView() {
        textView = (TextView) findViewById(R.id.main_textview);

    }
}










  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值