retrofit应用

原创 2018年04月15日 20:53:42
导依赖
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) {

            }
        });

    }
}








retrofit封装

  • 2017年09月17日 16:36
  • 25.81MB
  • 下载

Android Retrofit 2.0 的详细使用

文章转载自:Android Retrofit 2.0 的详细 使用攻略(含实例讲解) 前言 在Andrroi...
  • qq_40543575
  • qq_40543575
  • 2018-01-27 21:56:15
  • 1944

retrofit下载apk文件

  • 2017年09月01日 16:09
  • 220KB
  • 下载

Retrofit使用教程(一)- Retrofit入门详解

本篇博客主要讲解以下问题 - Retrofit简介 - Retrofit的简单使用例子 - Retrofit的get请求 - Retrofit的put请求(提交表单数据) - 如何为 retrofit...
  • gdutxiaoxu
  • gdutxiaoxu
  • 2016-10-06 20:38:34
  • 2985

一种类型安全的Java HTTP客户端库Retrofit

一种类型安全的Java HTTP客户端库 Retrofit介绍 Retrofit是一个开源的、类型安全的HTTP客户端,它适用于Android和Java平台,官方主页为:http://square.g...
  • chszs
  • chszs
  • 2016-03-18 10:13:46
  • 2470

rxjava retrofit 框架

  • 2018年01月05日 15:40
  • 141KB
  • 下载

retrofit2.3.0 学习笔记

retrofit2.3.0 学习笔记概述 参考 http://blog.csdn.net/ljd2038/article/details/51046512 http://www.jianshu.co...
  • undergrowth
  • undergrowth
  • 2017-08-22 22:28:47
  • 840

Android应用架构之Retrofit使用

Retrofit:Square提供的开源产品,为Android平台的应用提供一个类型安全的REST客户端         https://github.com/square/retrofit Rx...
  • liuhongwei123888
  • liuhongwei123888
  • 2015-12-21 23:42:28
  • 97391

retrofit学习、retrofit rxjava封装、retrofit mvp rxjava

  • 2016年11月15日 16:23
  • 10.83MB
  • 下载

在WebService中使用Retrofit+RxJava

最近Retrofit+RxJava还是非常火的,搭配一下MVP那开发简直就一个字爽。 但是现在很多比较老一点的公司还在使用WebService+xml,真的很让人感到忧伤。不过真的想使用Retrof...
  • Fancy_xty
  • Fancy_xty
  • 2016-07-06 10:48:37
  • 2656
收藏助手
不良信息举报
您举报文章:retrofit应用
举报原因:
原因补充:

(最多只允许输入30个字)