retrofit应用

导依赖
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
 compile 'com.squareup.retrofit2:converter-gson:2.4.0'
compile 'com.squareup.okhttp3:okhttp:3.1.2'


接口
ApiInter


package com.example.com.ratrofit;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;

/**
 * Created by linmeihui on 2018/4/13.
 */

public interface ApiInter {
    @GET("user/getUserInfo")
    Call<MessageBean<LoginInfo>> getInfo(@Query("uid") String id);

    @GET("search/query/listview/category/Android/count/{count}/page/{page}")
    Call<MessageData<List<News>>> getNews(@Path("count") String count, @Path("page") String page);

    @POST("")
    @Headers({"source:android"})
    Call<String> callPost(@Header("source") String header);
}


类LoginInfo

package com.example.com.ratrofit;

import com.google.gson.annotations.SerializedName;

/**
 * Created by linmeihui on 2018/4/13.
 */

public class LoginInfo {

    private Object age;
    private Object appkey;
    private String appsecret;
    private String createtime;
    private Object email;
    private int fans;
    private int follow;
    private int gender;
    private String icon;
    private Object latitude;
    private Object longitude;
    private String mobile;
    private int money;
    private String nickname;
    private String password;
    private Object praiseNum;
    private String token;
    private int uid;
    private Object userId;
    @SerializedName("username")
    private String userName;

    public Object getAge() {
        return age;
    }

    public void setAge(Object age) {
        this.age = age;
    }

    public Object getAppkey() {
        return appkey;
    }

    public void setAppkey(Object appkey) {
        this.appkey = appkey;
    }

    public String getAppsecret() {
        return appsecret;
    }

    public void setAppsecret(String appsecret) {
        this.appsecret = appsecret;
    }

    public String getCreatetime() {
        return createtime;
    }

    public void setCreatetime(String createtime) {
        this.createtime = createtime;
    }

    public Object getEmail() {
        return email;
    }

    public void setEmail(Object email) {
        this.email = email;
    }

    public int getFans() {
        return fans;
    }

    public void setFans(int fans) {
        this.fans = fans;
    }

    public int getFollow() {
        return follow;
    }

    public void setFollow(int follow) {
        this.follow = follow;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Object getLatitude() {
        return latitude;
    }

    public void setLatitude(Object latitude) {
        this.latitude = latitude;
    }

    public Object getLongitude() {
        return longitude;
    }

    public void setLongitude(Object longitude) {
        this.longitude = longitude;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Object getPraiseNum() {
        return praiseNum;
    }

    public void setPraiseNum(Object praiseNum) {
        this.praiseNum = praiseNum;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public Object getUserId() {
        return userId;
    }

    public void setUserId(Object userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}


类News

package com.example.com.ratrofit;

import com.google.gson.annotations.SerializedName;

/**
 * Created by linmeihui on 2018/4/13.
 */

public class News {

    /**
     * desc : 还在用ListView     * ganhuo_id : 57334c9d67765903fb61c418
     * publishedAt : 2016-05-12T12:04:43.857000
     * readability :
     * type : Android
     * url : http://www.jianshu.com/p/a92955be0a3e
     * who : 陈宇明
     */

    private String desc;
    @SerializedName("ganhuo_id")
    private String ganhuoId;
    private String publishedAt;
    private String readability;
    private String type;
    private String url;
    private String who;

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getGanhuoId() {
        return ganhuoId;
    }

    public void setGanhuoId(String ganhuoId) {
        this.ganhuoId = ganhuoId;
    }

    public String getPublishedAt() {
        return publishedAt;
    }

    public void setPublishedAt(String publishedAt) {
        this.publishedAt = publishedAt;
    }

    public String getReadability() {
        return readability;
    }

    public void setReadability(String readability) {
        this.readability = readability;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getWho() {
        return who;
    }

    public void setWho(String who) {
        this.who = who;
    }
}


类MessageData

package com.example.com.ratrofit;

/**
 * Created by linmeihui on 2018/4/13.
 */


public class MessageData<T> {
    private int count;
    private boolean error;
    private T results;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public boolean isError() {
        return error;
    }

    public void setError(boolean error) {
        this.error = error;
    }

    public T getResults() {
        return results;
    }

    public void setResults(T results) {
        this.results = results;
    }
}



类 MessageBean

package com.example.com.ratrofit;

import com.google.gson.annotations.SerializedName;

/**
 * Created by linmeihui on 2018/4/13.
 */

public class MessageBean<T> {
    @SerializedName("msg")
    private String message;
    private String code;
    private T data;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}


MainActivity


package com.example.com.ratrofit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String URL = "https://www.zhaoapi.cn/user/getUserInfo?uid=71";
    private static final String TAG = "RetrofitActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void request(View view) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://gank.io/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiInter apiInter = retrofit.create(ApiInter.class);

        Call<MessageData<List<News>>> news = apiInter.getNews("9", "2");

        news.enqueue(new Callback<MessageData<List<News>>>() {
            @Override
            public void onResponse(Call<MessageData<List<News>>> call, Response<MessageData<List<News>>> response) {

                List<News> results = response.body().getResults();

                if (results != null) {
                    Log.i(TAG, "获取的新闻条目数量是: " + results.size());
                } else {
                    Log.i(TAG, "获取的新闻条目数量是: 0");
                }

            }

            @Override
            public void onFailure(Call<MessageData<List<News>>> call, Throwable t) {

            }
        });

    }
}








阅读更多
想对作者说点什么?
相关热词

博主推荐

换一批

没有更多推荐了,返回首页