Rtrofit展示数据

本文详细介绍了如何在Android应用中使用Retrofit库来加载数据。首先,添加Retrofit依赖,接着创建布局和子布局,定义Bean类以封装数据,然后利用工具类进行网络请求配置,并集成Fresco库来优化图片加载。最后,别忘了为Adapter设置正确的name属性。
摘要由CSDN通过智能技术生成
首先展示效果图


添加权限
<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

添加依赖

compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    compile 'io.reactivex:rxandroid:1.2.0'
    //fresco的依赖包
    implementation 'com.facebook.fresco:fresco:1.8.1'
    //recyclerview的依赖包
    implementation 'com.android.support:design:27.0.2'

布局页面

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

子布局

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

        >
        <com.facebook.drawee.view.SimpleDraweeView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/img"
            />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/titall"
                android:textColor="#f00"
                android:textSize="20sp"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/time_"
                android:text="2017-05-23  14:25"
                android:textSize="15sp"
                />
        </LinearLayout>
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
       android:id="@+id/sim_img"
        android:src="@drawable/cha"
        android:layout_alignParentRight="true"
        />
</RelativeLayout>
    </LinearLayout>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tv_nr"
    />

</LinearLayout>

bean类

http://v.juhe.cn/weixin/query?key=88f7bbc507e3ecacfaeab2b47dd8936f

工具类

GitHubService

package com.example.zld.retrofittext.utils;

import com.example.zld.retrofittext.bean.Bean;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface GitHubService {
    //http://v.juhe.cn/weixin/query?key=88f7bbc507e3ecacfaeab2b47dd8936f
    @GET("weixin/query")
    Call<Bean>get(@Query("key")String key);
}

配置fresco

记得加name

IApp


package com.example.zld.retrofittext;
import android.app.Application;
import com.facebook.drawee.backends.pipeline.Fresco;
public class IApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}

适配器

package com.example.zld.retrofittext.Adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.zld.retrofittext.R;
import com.example.zld.retrofittext.bean.Bean;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;
public class IAdapter  extends RecyclerView.Adapter<IViewHoder>{
    private Context context;
    private List<Bean.ResultBean.ListBean>list;
    public IAdapter(Context context, List<Bean.ResultBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public IViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item, null);

        return new IViewHoder(view);
    }
    @Override
    public void onBindViewHolder(IViewHoder holder, int position) {
     holder.img.setImageURI(list.get(position).getFirstImg());
     holder.titall.setText(list.get(position).getSource());
     holder.tv_nr.setText(list.get(position).getTitle());
    }
    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }
}
class IViewHoder extends RecyclerView.ViewHolder{
      SimpleDraweeView img;
      TextView titall;
      TextView tv_nr;

    public IViewHoder(View itemView) {
        super(itemView);
        img = itemView.findViewById(R.id.img);
        titall = itemView.findViewById(R.id.titall);
        tv_nr = itemView.findViewById(R.id.tv_nr);
    }
}
MainActivity
package com.example.zld.retrofittext;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.example.zld.retrofittext.Adapter.IAdapter;
import com.example.zld.retrofittext.bean.Bean;
import com.example.zld.retrofittext.utils.GitHubService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerview = findViewById(R.id.recy);
         Retrofit retrofit = new Retrofit.Builder()
                 .baseUrl("http://v.juhe.cn")
                 .addConverterFactory(GsonConverterFactory.create())
                 .build();
        GitHubService service = retrofit.create(GitHubService.class);
        Call<Bean> call = service.get("88f7bbc507e3ecacfaeab2b47dd8936f");
        call.enqueue(new Callback<Bean>() {
            @Override
            public void onResponse(Call<Bean> call, Response<Bean> response) {
                Bean bean = response.body();
                System.out.println("bean------");
                LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
                recyclerview.setLayoutManager(manager);
                IAdapter adapter = new IAdapter(MainActivity.this, bean.getResult().getList());
                recyclerview.setAdapter(adapter);

            }
            @Override
            public void onFailure(Call<Bean> call, Throwable t) {
            }
        });
    }
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值