retrofit网络请求

    //依赖
    implementation 'com.squareup.retrofit2:retrofit:2.0.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
    implementation 'com.squareup.okio:okio:1.5.0'
    implementation 'com.squareup.okhttp3:okhttp:3.2.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    //权限
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
//创建Api类
public class Api {
    //http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1
    public static final String wealUrl ="http://gank.io/api/";

    //http://www.93.gov.cn/93app/data.do?channelId=1&startNum=1
    public static final String newsUrl ="http://www.93.gov.cn/93app/";
    //http://172.17.8.100/small/user/v1/login
    public static final String loginUrl ="http://172.17.8.100/";
}


在这里插入图片描述

//创建接口ApiService 
public interface ApiService {
    //http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1
    @GET("data/%E7%A6%8F%E5%88%A9/10/{page}")
    Call<WealInfo> getWeal(@Path("page") int page);

    //http://www.93.gov.cn/93app/data.do?channelId=1&startNum=1

    @GET("data.do")
    Call<NewsInfo> getNews(@Query("channelId") int channelId, @Query("startNum") int startNum);

    //http://172.17.8.100/small/user/v1/login

    //登录post请求
    @POST("small/user/v1/login")
    @FormUrlEncoded
    Call<LoginInfo> login(@Field("phone") String phone, @Field("pwd") String pwd);

    //上传头像


}

//activity

public class MainActivity extends AppCompatActivity {
    private int page = 1;

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

        //getServerData();
        // getNewsData();

        login();
    }


    private void getServerData() {
        //创建Retrofit
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.wealUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //java动态代理的方式得到代理对象
        ApiService apiService = retrofit.create(ApiService.class);
        //通过代理对象执行get请求返回Call
        Call<WealInfo> call = apiService.getWeal(page);

        call.enqueue(new Callback<WealInfo>() {
            @Override
            public void onResponse(Call<WealInfo> call, Response<WealInfo> response) {
                WealInfo wealInfo = response.body();
                Log.i("xxx", wealInfo.results.size() + "");
            }

            @Override
            public void onFailure(Call<WealInfo> call, Throwable t) {

            }
        });

    }

    private void getNewsData() {

        //创建Retrofit
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.newsUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //java动态代理的方式得到代理对象
        ApiService apiService = retrofit.create(ApiService.class);

//通过代理对象执行get请求返回Call
        Call<NewsInfo> call = apiService.getNews(1, 1);
        call.enqueue(new Callback<NewsInfo>() {
            //此方法在主线程
            @Override
            public void onResponse(Call<NewsInfo> call, Response<NewsInfo> response) {

                NewsInfo newsInfo = response.body();
                NewsInfo.DataInfo dataInfo = newsInfo.data.get(0);
                String title = dataInfo.getTITLE();
                Log.i("xxx", title);
            }

            @Override
            public void onFailure(Call<NewsInfo> call, Throwable t) {

            }
        });


    }

    private void login() {
        //创建日志拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("xxx", message);
            }
        });
        //设置日志拦截器模式
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        //创建OkHttpClient
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                
                .build();

    //创建Retrofit
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.loginUrl)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //java动态代理的方式得到代理对象
        ApiService apiService = retrofit.create(ApiService.class);

   //通过代理对象执行get请求返回Call
        Call<LoginInfo> call = apiService.login("18810609754", "123");
        call.enqueue(new Callback<LoginInfo>() {
            @Override
            public void onResponse(Call<LoginInfo> call, Response<LoginInfo> response) {
                LoginInfo loginInfo = response.body();
                String nickName = loginInfo.result.getNickName();
                Log.i("xxx", nickName);
            }

            @Override
            public void onFailure(Call<LoginInfo> call, Throwable t) {

            }
        });
    }

}


三个接口的Bean类

//福利的Bean类
public class WealInfo {
    public List<resultsInfo> results;


    public class resultsInfo {
        public String url;
        public String who;

        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;
        }
    }

}

//新闻的bean类
public class NewsInfo {

    public List<DataInfo> data;

    public class DataInfo {
        public String TITLE;
        public String IMAGEURL;

        public String getTITLE() {
            return TITLE;
        }

        public void setTITLE(String TITLE) {
            this.TITLE = TITLE;
        }

        public String getIMAGEURL() {
            return IMAGEURL;
        }

        public void setIMAGEURL(String IMAGEURL) {
            this.IMAGEURL = IMAGEURL;
        }
    }
}

//登录的bean类
public class NewsInfo {

    public List<DataInfo> data;

    public class DataInfo {
        public String TITLE;
        public String IMAGEURL;

        public String getTITLE() {
            return TITLE;
        }

        public void setTITLE(String TITLE) {
            this.TITLE = TITLE;
        }

        public String getIMAGEURL() {
            return IMAGEURL;
        }

        public void setIMAGEURL(String IMAGEURL) {
            this.IMAGEURL = IMAGEURL;
        }
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值