先看一眼效果图:
那我们闲话少说,直接上代码了。
首先我们要在相对应的 build.gradle 文件中导入相应的依赖
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.picasso:picasso:2.71828'
然后在清单文件中加入网络访问权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
接下来是布局
主布局 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
然后 Recyclerview 中的每一个 item 项的子布局 xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:padding="1dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center_vertical"
android:text="@string/app_name"
android:textSize="16sp"
android:textColor="#000"
android:textStyle="bold" />
<TextView
android:id="@+id/str"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/MI" />
<TextView
android:id="@+id/food_str"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/app_name" />
</LinearLayout>
</LinearLayout>
然后我们要去生成一个 JavaBean 的实体类:
package com.example.retrofit;
import java.util.List;
public class Bean {
private int ret;
private List<DataBean> data;
public int getRet() {
return ret;
}
public void setRet(int ret) {
this.ret = ret;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String id;
private String title;
private String pic;
private String collect_num;
private String food_str;
private int num;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getCollect_num() {
return collect_num;
}
public void setCollect_num(String collect_num) {
this.collect_num = collect_num;
}
public String getFood_str() {
return food_str;
}
public void setFood_str(String food_str) {
this.food_str = food_str;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
}
这里推荐把从网址上拿下来的字符串,先进行解析,笔者使用的是GsonFormat的插件,可以对数据一键解析,具体推荐使用GsonFormat的插件,快捷键 ctrl+S。
创建完实体类就可准备请求数据了,先写一个接口:
package com.example.retrofit;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Url;
public interface IRetrofitService {
@GET
Call<Bean> getUrl(@Url String url);
}
然后我们这里在准备一个 base_url
package com.example.retrofit;
public class Contant {
public static final String BASE_URL ="http://www.qubaobei.com";
}
这里可有部分朋友理解不了,这样写的好处是,当公司正式的项目没有上线时,有可能使用的公网的地址,还不是正式的域名,单独把 BASE_URL 提出来的好处也是为了日后方便修改,不用在去代码海里去挨个找寻。
下边是具体的请求数据的部分:
private void initData(int stage_id) {
String nurl="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id="+ stage_id +"&limit=20&page=1";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Contant.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
IRetrofitService retrofitService=retrofit.create(IRetrofitService.class);
Call<Bean> call = retrofitService.getUrl(nurl);
call.enqueue(new Callback<Bean>() {
@Override
public void onResponse(Call<Bean> call, Response<Bean> response) {
Bean bean = response.body();
arrayList.clear();
arrayList.addAll(bean.getData());
recyclerViewAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<Bean> call, Throwable t) {
t.printStackTrace();
}
});
}
完整的 MainActivity 类
package com.example.retrofit;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
private List<Bean.DataBean> arrayList;
private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;
private SwipeRefreshLayout swipeRefreshLayout;
int stage_id;
private String url="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData(1);
}
private void initView() {
stage_id = 1;
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
arrayList = new ArrayList<>();
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
recyclerViewAdapter=new RecyclerViewAdapter(getApplicationContext(),arrayList);
recyclerView.setAdapter(recyclerViewAdapter);
swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.white);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
if (stage_id<8){
stage_id = stage_id + 1;
}else {
stage_id = 1;
}
Log.d("lcj","stage_id = "+stage_id);
initData(stage_id);
swipeRefreshLayout.setRefreshing(false);
Toast.makeText(MainActivity.this,"刷新成功",Toast.LENGTH_SHORT).show();
}
});
}
private void initData(int stage_id) {
String nurl="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id="+ stage_id +"&limit=20&page=1";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Contant.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
IRetrofitService retrofitService=retrofit.create(IRetrofitService.class);
Call<Bean> call = retrofitService.getUrl(nurl);
call.enqueue(new Callback<Bean>() {
@Override
public void onResponse(Call<Bean> call, Response<Bean> response) {
Bean bean = response.body();
arrayList.clear();
arrayList.addAll(bean.getData());
recyclerViewAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<Bean> call, Throwable t) {
t.printStackTrace();
}
});
}
}
然后是最后一步,我们已经拿到数据并且将它存入集合当中,剩下要做的事就是将数据展示出来,recyclerview 的使用,就是通过适配器来完成的。
package com.example.retrofit;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyHolder>{
private Context context;
private List<Bean.DataBean> data;
public RecyclerViewAdapter(Context context, List<Bean.DataBean> data) {
this.context = context;
this.data = data;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = LayoutInflater.from(context).inflate(R.layout.item, viewGroup, false);
return new MyHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
Picasso.get().load(data.get(i).getPic()).into(myHolder.im);
myHolder.title.setText(data.get(i).getTitle());
myHolder.food_str.setText(data.get(i).getFood_str());
}
@Override
public int getItemCount() {
return data.size();
}
public void refresh(List<Bean.DataBean> list){
this.data.addAll(list);
notifyDataSetChanged();
Log.d("lcj","notifyDataSetChanged");
}
class MyHolder extends RecyclerView.ViewHolder {
ImageView im;
TextView title;
TextView food_str;
public MyHolder(@NonNull View itemView) {
super(itemView);
im=itemView.findViewById(R.id.imageView);
title=itemView.findViewById(R.id.title);
food_str=itemView.findViewById(R.id.food_str);
}
}
}
安卓 9 加强了保密性,导致无法进行非加密的 http 网络请求
解决方法:
在 AndroidManifest.xml 的 application 标签中新加:
android:usesCleartextTraffic=“true”
重新运行即可;
结语: 希望路过的人能有所收获,这只是,最最简单的基本使用,大佬勿喷,有错误和不足还请指正!
|