Retrofit请求网络解析json
懒得一大堆废话,说完实现最终目标,直接进入正题操作,直接上代码,看使用方法
实现目标:用Retrofit解析网络json,放到RecyclerView中
第一步,写好布局文件以及item,我这里用的RecyclerView,不知道RecyclerView就点进去看看,解析出来的格式就是,一个图片,一个文字,直接看布局文件和item。
加入依赖
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
加入RecyclerView
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout 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">
-
-
<android.support.v7.widget.RecyclerView
-
android:id="@+id/recyclerView"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
-
-
</LinearLayout>
item:
-
<?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">
-
-
<ImageView
-
android:id="@+id/imageView"
-
android:layout_width="0dp"
-
android:layout_height="120dp"
-
android:layout_weight="2" />
-
-
<TextView
-
android:id="@+id/textView"
-
android:layout_width="0dp"
-
android:layout_height="120dp"
-
android:layout_weight="3"
-
android:gravity="center" />
-
-
</LinearLayout>
之后写一个RecyclerView的适配器,用具加入图片和文字
-
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyHolder> {
-
-
private Context context;
-
private List<JavaBean.DataBean> data;//数据源集合
-
-
//构造方法
-
public RecyclerViewAdapter(Context context, List<JavaBean.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.with(context).load(data.get(i).getPic()).into(myHolder.imageView);//加入图片
-
myHolder.textView.setText(data.get(i).getTitle());//加入名字
-
}
-
-
@Override
-
public int getItemCount() {
-
return data.size();//返回集合size
-
}
-
-
public class MyHolder extends RecyclerView.ViewHolder {
-
-
ImageView imageView;
-
TextView textView;
-
-
public MyHolder(@NonNull View itemView) {
-
super(itemView);
-
imageView = itemView.findViewById(R.id.imageView);
-
textView = itemView.findViewById(R.id.textView);
-
}
-
}
-
}
提供网络字符串:http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1
写出解析类JavaBean类,即解析类!
接下来是重点,如何通过Retrofit获取网络数据,看java代码
-
Retrofit retrofit = new Retrofit.Builder()
-
.baseUrl("https://1604a.com/data/")//网络数据源,网址前半段
-
.addConverterFactory(GsonConverterFactory.create())//解析
-
.build();
创建接口,get请求方式,写入网址的后半段,即最后一个“/”之后的字符,像下面这么写
-
public interface MyInterface {
-
@GET("ymdx.json")
-
Call<JavaBean> gerRequest();
-
}
接着使用这个接口
-
MyInterface myInterface = retrofit.create(MyInterface.class);
-
Call<JavaBean> call = myInterface.gerRequest();
接下来写Call的一个方法enqueue,注释有详细标明
-
call.enqueue(new Callback<JavaBean>() {
-
-
//成功
-
@Override
-
public void onResponse(Call<JavaBean> call, Response<JavaBean> response) {
-
//解析添加到创建的集合
-
arrayList = response.body().getData();
-
-
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
-
recyclerView.setItemAnimator(new DefaultItemAnimator());//RecyclerView动画:默认动画
-
//使用适配器
-
recyclerView.setAdapter(new RecyclerViewAdapter(MainActivity.this,arrayList));
-
-
-
}
-
-
//失败
-
@Override
-
public void onFailure(Call<JavaBean> call, Throwable t) {
-
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
-
}
-
});
看一下完整的java代码
-
public class MainActivity extends AppCompatActivity {
-
-
private List<JavaBean.DataBean> arrayList;
-
private RecyclerView recyclerView;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
initView();
-
-
Retrofit retrofit = new Retrofit.Builder()
-
.baseUrl("https://1604a.com/data/")
-
.addConverterFactory(GsonConverterFactory.create())
-
.build();
-
-
MyInterface myInterface = retrofit.create(MyInterface.class);
-
Call<JavaBean> call = myInterface.gerRequest();
-
-
call.enqueue(new Callback<JavaBean>() {
-
-
//成功
-
@Override
-
public void onResponse(Call<JavaBean> call, Response<JavaBean> response) {
-
//解析添加到创建的集合
-
arrayList = response.body().getData();
-
-
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
-
recyclerView.setItemAnimator(new DefaultItemAnimator());
-
//使用适配器
-
recyclerView.setAdapter(new RecyclerViewAdapter(MainActivity.this,arrayList));
-
-
-
}
-
-
//失败
-
@Override
-
public void onFailure(Call<JavaBean> call, Throwable t) {
-
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
-
}
-
});
-
-
-
}
-
-
private void initView() {
-
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
-
}
-
}
》》》》快速实例化控件《《《《
最重要的一步,加入网络权限:<uses-permission android:name="android.permission.INTERNET"/>
效果浏览:
文章来源: blog.csdn.net,作者:第三女神程忆难,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_40881680/article/details/82797370
- 点赞
- 收藏
- 关注作者
评论(0)