https://gitee.com/olleh/RealTest.git
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
def retrofit_version = "2.1.0"
def rxjava_version = "2.0.1"
//RxJava的依赖包
implementation 'io.reactivex.rxjava2:rxjava:' + rxjava_version
//RxAndroid的依赖包
implementation 'io.reactivex.rxjava2:rxandroid:' + rxjava_version
//Rxjava + retrofit 用到的一个依赖库,使接口回调的数据以Rxjava形式供我们接受
implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0-RC3'
//retrofig依赖包
implementation 'com.squareup.retrofit2:retrofit:' + retrofit_version
//自动将服务器返回的数据 以及 我们传给一个JavaBeen实体,转换成json
implementation 'com.squareup.retrofit2:converter-gson:' + retrofit_version
//gson实体解析库
implementation 'com.google.code.gson:gson:2.6.2'
//OKHttp日志拦截器,OKHttp官方提供。
implementation 'com.squareup.okhttp3:logging-interceptor:3.3.0'
//https://github.com/DingMouRen/LayoutManagerGroup
implementation 'com.github.DingMouRen:LayoutManagerGroup:1e6f4f96eb'//maven { url 'https://jitpack.io' }
//https://muyangmin.github.io/glide-docs-cn/
implementation 'com.github.bumptech.glide:glide:4.11.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
}
package com.zhangyu.realtest.net;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.gson.Gson;
import com.zhangyu.realtest.bean.RealPic;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkHttpManager implements NetManager {
private static final String TAG = "OkHttpManager";
//1.公开的getInstance
public static OkHttpManager getInstance() {
return Holder.instance;
}
//2.私有的持有类
private static class Holder {
private final static OkHttpManager instance = new OkHttpManager();
}
//3.私有的构造方法
private OkHttpManager() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(15, TimeUnit.SECONDS);
okHttpClient = builder.build();
}
private OkHttpClient okHttpClient;
private Handler handler = new Handler(Looper.getMainLooper());
@Override
public void getPicList(String name, int startNum, int pageNum, @NonNull final NetCallBack netCallBack) {
Request.Builder builder = new Request.Builder();
//http://image.so.com/j?q=汉堡包&sn=0&pn=50
builder.url("http://image.so.com/" + "j?q=" + name + "&sn=" + startNum + "&pn=" + pageNum);
Request request = builder.get().build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
Log.e(TAG, "onFailure: " + e.getMessage());
handler.post(new Runnable() {
@Override
public void run() {
netCallBack.onFailed(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String data = response.body().string();
Log.e(TAG, "onResponse: " + data);
// 使用new方法
Gson gson = new Gson();
// fromJson 将json字符串转为bean对象
final RealPic realPic= gson.fromJson(data, RealPic.class);
handler.post(new Runnable() {
@Override
public void run() {
netCallBack.onSuccess(realPic);
}
});
}
});
}
}
package com.zhangyu.realtest.net;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.zhangyu.realtest.bean.RealPic;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitManager implements NetManager {
private static final String TAG = "RetrofitManager";
RealApi realApi;
private RetrofitManager() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://image.so.com/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
realApi = retrofit.create(RealApi.class);
}
public static RetrofitManager getInstance() {
return SingletonHolder.mInstance;
}
private static class SingletonHolder {
private static final RetrofitManager mInstance = new RetrofitManager();
}
@Override
public void getPicList(String name, int startNum, int pageNum, final NetCallBack netCallBack) {
realApi.getPicData(name, startNum, pageNum)
.enqueue(new Callback<RealPic>() {
@Override
public void onResponse(Call<RealPic> call, Response<RealPic> response) {
netCallBack.onSuccess(response.body());
}
@Override
public void onFailure(Call<RealPic> call, Throwable t) {
netCallBack.onFailed(t);
}
});
}
}
package com.zhangyu.realtest.net;
import com.zhangyu.realtest.bean.RealPic;
import io.reactivex.Observable;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface RealApi {
/**
* http://image.so.com/j?q=汉堡包&sn=0&pn=50
* (query关键词:汉堡包,开始序号sn=0,item数pn=50)
*/
@GET("j")
Observable<ResponseBody> getPicDataRx(@Query("q") String name,
@Query("sn") int sn,
@Query("pn") int pn
);
@GET("j")
Call<RealPic> getPicData(@Query("q") String name,
@Query("sn") int sn,
@Query("pn") int pn
);
}
package com.zhangyu.realtest.adapter;
import android.content.Context;
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.bumptech.glide.Glide;
import com.zhangyu.realtest.R;
import com.zhangyu.realtest.bean.RealPic;
import java.util.List;
public class RealPicAdapter extends RecyclerView.Adapter<RealPicAdapter.ViewHolder> {
private Context context;
public RealPicAdapter(Context context) {
this.context = context;
}
private List<RealPic.ListBean> dataList;
public void setDataList(List<RealPic.ListBean> dataList) {
this.dataList = dataList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_real_pic, parent, false);
ViewHolder holder = new ViewHolder(view);
//点击事件
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
RealPic.ListBean listBean = dataList.get(position);
Glide.with(context)
.load(listBean.getThumb())
.centerCrop()
.into(holder.imageView);
holder.textView.setText(listBean.getTitle());
}
@Override
public int getItemCount() {
if (dataList != null) {
return dataList.size();
}
return 0;
}
static class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
ViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.iv_pic);
textView = itemView.findViewById(R.id.tv_desc);
}
}
}
package com.zhangyu.realtest.net;
public class NetUtil {
//1.公开的获取单例方法
public static NetUtil getInstance() {
return Holder.instance;
}
//2.私有的持有类
private static class Holder {
private static final NetUtil instance = new NetUtil();
}
//3.私有的构造方法
private NetUtil() {
}
private NetManager netManager = RetrofitManager.getInstance();
public void getData(String name, int startNum, int pageNum, NetCallBack netCallBack) {
netManager.getPicList(name, startNum, pageNum, netCallBack);
}
}
package com.zhangyu.realtest;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import com.dingmouren.layoutmanagergroup.echelon.EchelonLayoutManager;
import com.zhangyu.realtest.adapter.RealPicAdapter;
import com.zhangyu.realtest.bean.RealPic;
import com.zhangyu.realtest.net.NetCallBack;
import com.zhangyu.realtest.net.NetUtil;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private RecyclerView rvPic;
private RealPicAdapter realPicAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
NetUtil.getInstance().getData("饮料", 0, 50, new NetCallBack() {
@Override
public void onSuccess(RealPic realPic) {
List<RealPic.ListBean> list = realPic.getList();
realPicAdapter = new RealPicAdapter(MainActivity.this);
realPicAdapter.setDataList(list);
rvPic.setAdapter(realPicAdapter);
rvPic.setLayoutManager(new EchelonLayoutManager(MainActivity.this));
}
@Override
public void onFailed(Throwable throwable) {
Log.e(TAG, "onFailure: " + throwable.toString());
}
});
}
private void initView() {
rvPic = (RecyclerView) findViewById(R.id.rv_pic);
}
}