美文网首页
[实例]使用Retrofit请求展示在RecycleView中使

[实例]使用Retrofit请求展示在RecycleView中使

作者: Merbng | 来源:发表于2019-11-13 18:21 被阅读0次
demo效果

1.导入依赖

    implementation 'com.squareup.retrofit2:retrofit:2.3.0'//网络请求相关
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'//网络请求相关
    implementation "com.android.support:recyclerview-v7:28.0.0"//列表控件
    implementation 'com.github.bumptech.glide:glide:3.7.0'//加载图片

2.编写布局&代码

  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.merbng.piclistdemo.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:layout_width="250dp"
            android:layout_height="50dp"
            android:layout_marginBottom="20dp"
            android:hint="请输入搜索关键字" />

        <Button
            android:layout_width="wrap_content"
            android:onClick="searchClick"
            android:layout_height="wrap_content"
            android:text="搜索" />
    </LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

  • item_pic_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/img_cover"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:padding="10dp"
        android:src="@drawable/ic_cover" />
</LinearLayout>
  • MainActivity.java
package com.merbng.piclistdemo;

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

import java.util.ArrayList;
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 String TAG = "=========";//日志标记

    private RecyclerView recyclerView;
    private PicListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = ((RecyclerView) findViewById(R.id.rv));
        recyclerView.setLayoutManager(new GridLayoutManager(this,3));
        adapter = new PicListAdapter(this, null);
    }
    public void searchClick(View view){
        getPicList();
    }

    private void getPicList() {
        //创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://yapi.demo.qunar.com/mock/35238/testurl/") //基础url,其他部分在GetRequestInterface里
                .addConverterFactory(GsonConverterFactory.create()) //Gson数据转换器
                .build();
        //创建网络请求接口实例
        GetRequestInterface request = retrofit.create(GetRequestInterface.class);
        Call<PicListBean> call = request.getList();
        //发送网络请求(异步)
        call.enqueue(new Callback<PicListBean>() {
            @Override
            public void onResponse(Call<PicListBean> call, Response<PicListBean> response) {
                PicListBean data = response.body();
                List<PicListBean.DataEntity> list = data.getData();
                ArrayList<String> urlList = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    urlList.add(list.get(i).getPicUrl());
                }
                adapter.setData(urlList);
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<PicListBean> call, Throwable t) {
                Log.e(TAG, t.toString());
            }
        });
    }
}

  • GetRequestInterface.java
package com.merbng.piclistdemo;

import retrofit2.Call;
import retrofit2.http.GET;

/**
 * Created by merbng on 2019/11/13.
 */

public interface GetRequestInterface {
    /**
     * 获取列表
     * */
    @GET("testlist")
    Call<PicListBean> getList();
}

  • PicListBean.java
package com.merbng.piclistdemo;

import java.util.List;

/**
 * Created by merbng on 2019/11/13.
 */

public class PicListBean {
 
    private String result;
    private List<DataEntity> data;
    private String message;

    public void setResult(String result) {
        this.result = result;
    }

    public void setData(List<DataEntity> data) {
        this.data = data;
    }

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

    public String getResult() {
        return result;
    }

    public List<DataEntity> getData() {
        return data;
    }

    public String getMessage() {
        return message;
    }

    public class DataEntity {
        /**
         * picUrl : http://ylbb-app-mobile01.oss-cn-beijing.aliyuncs.com/82773fff-2ffe-41e8-8d60-d9ff8ec69b4b.jpg
         * productId : 1
         * categoryId : 2
         */
        private String picUrl;
        private String productId;
        private String categoryId;

        public void setPicUrl(String picUrl) {
            this.picUrl = picUrl;
        }

        public void setProductId(String productId) {
            this.productId = productId;
        }

        public void setCategoryId(String categoryId) {
            this.categoryId = categoryId;
        }

        public String getPicUrl() {
            return picUrl;
        }

        public String getProductId() {
            return productId;
        }

        public String getCategoryId() {
            return categoryId;
        }
    } 
}

demo结构.png

最后不要忘记添加网络权限<uses-permission android:name="android.permission.INTERNET" />
参考链接
Glide图片加载框架基本使用
Android使用Retrofit进行网络请求

相关文章

网友评论

      本文标题:[实例]使用Retrofit请求展示在RecycleView中使

      本文链接:https://www.haomeiwen.com/subject/vqjkictx.html