1、首先先导入需要用到的依赖包
//recyclerview用到的依賴包
compile 'com.android.support:recyclerview-v7:25.1.1'
//网络请求依赖包
compile 'net.qiujuer.common:okhttp:3.0.0'
2、在布局文件里面设置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"
android:orientation="vertical"
tools:context="com.example.administrator.okhttpdemo2.activity.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>
3、写一个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">
<TextView
android:id="@+id/texts"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#f0f"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
然后开始我们的代码工作
4、写需要用到的Bean(如果需要请求的数据多,需要什么数据就添加什么数据,不需要的可以不写,为了方便我只请求了一个数据条)
package com.example.administrator.okhttpdemo2.bean;
import java.util.List;
/**
* Created by Administrator on 2017/5/25.
*/
public class Bean {
private int code;
private List<Data> data;
private String msg;
public Bean(int code, List<Data> data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public List<Data> getData() {
return data;
}
public void setData(List<Data> data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String toString() {
return "Bean{" +
"code=" + code +
", data=" + data +
", msg='" + msg + '\'' +
'}';
}
public class Data {
private int id;
private String efficacy;
private String goods_img;
public Data(int id, String efficacy, String goods_img) {
this.id = id;
this.efficacy = efficacy;
this.goods_img = goods_img;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEfficacy() {
return efficacy;
}
public void setEfficacy(String efficacy) {
this.efficacy = efficacy;
}
public String getGoods_img() {
return goods_img;
}
public void setGoods_img(String goods_img) {
this.goods_img = goods_img;
}
@Override
public String toString() {
return "Data{" +
"id=" + id +
", efficacy='" + efficacy + '\'' +
", goods_img='" + goods_img + '\'' +
'}';
}
}
}
5、设置我们的适配器(就是RecyclerView的适配器,和普通的展示String数据不一样,不过也相差不多,看代码)
package com.example.administrator.okhttpdemo2.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.administrator.okhttpdemo2.R;
import com.example.administrator.okhttpdemo2.bean.Bean;
/**
* Created by Administrator on 2017/5/25.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
private Context context;
private Bean bean;
public MyAdapter(Context context, Bean bean) {
this.context = context;
this.bean = bean;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyHolder holder = new MyHolder(LayoutInflater.from(context).inflate(R.layout.item, parent, false));
return holder;
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.texts.setText(bean.getData().get(position).getEfficacy());
}
@Override
public int getItemCount() {
return bean.getData().size();
}
class MyHolder extends RecyclerView.ViewHolder {
TextView texts;
public MyHolder(View itemView) {
super(itemView);
texts = (TextView) itemView.findViewById(R.id.texts);
}
}
}
6、添加已经封装好的OkHttp工具类,(不要嫌多,工具类直接粘过去用就行)
package com.example.administrator.okhttpdemo2.okhttp;
import android.os.Handler;
import android.os.Looper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* Created by fan on 2016/11/9.
*/
public class OkHttp {
/**
* 静态实例
*/
private static OkHttp sOkHttpManager;
/**
* okhttpclient实例
*/
private OkHttpClient mClient;
/**
* 因为我们请求数据一般都是子线程中请求,在这里我们使用了handler
*/
private Handler mHandler;
/**
* 构造方法
*/
private OkHttp() {
mClient = new OkHttpClient();
/**
* 在这里直接设置连接超时.读取超时,写入超时
*/
mClient.newBuilder().connectTimeout(10, TimeUnit.SECONDS);
mClient.newBuilder().readTimeout(10, TimeUnit.SECONDS);
mClient.newBuilder().writeTimeout(10, TimeUnit.SECONDS);
/**
* 如果是用的3.0之前的版本 使用以下直接设置连接超时.读取超时,写入超时
*/
//client.setConnectTimeout(10, TimeUnit.SECONDS);
//client.setWriteTimeout(10, TimeUnit.SECONDS);
//client.setReadTimeout(30, TimeUnit.SECONDS);
/**
* 初始化handler
*/
mHandler = new Handler(Looper.getMainLooper());
}
/**
* 单例模式 获取OkHttp实例
*
* @return
*/
public static OkHttp getInstance() {
if (sOkHttpManager == null) {
sOkHttpManager = new OkHttp();
}
return sOkHttpManager;
}
//-------------------------同步的方式请求数据--------------------------
/**
* 对外提供的get方法,同步的方式
*
* @param url 传入的地址
* @return
*/
public static Response getSync(String url) {
//通过获取到的实例来调用内部方法
return sOkHttpManager.inner_getSync(url);
}
/**
* GET方式请求的内部逻辑处理方式,同步的方式
*
* @param url
* @return
*/
private Response inner_getSync(String url) {
Request request = new Request.Builder().url(url).build();
Response response = null;
try {
//同步请求返回的是response对象
response = mClient.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
/**
* 对外提供的同步获取String的方法
*
* @param url
* @return
*/
public static String getSyncString(String url) {
return sOkHttpManager.inner_getSyncString(url);
}
/**
* 同步方法
*/
private String inner_getSyncString(String url) {
String result = null;
try {
/**
* 把取得到的结果转为字符串,这里最好用string()
*/
result = inner_getSync(url).body().string();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//-------------------------异步的方式请求数据--------------------------
public static void getAsync(String url, DataCallBack callBack) {
getInstance().inner_getAsync(url, callBack);
}
/**
* 内部逻辑请求的方法
*
* @param url
* @param callBack
* @return
*/
private void inner_getAsync(String url, final DataCallBack callBack) {
final Request request = new Request.Builder().url(url).build();
mClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
deliverDataFailure(request, e, callBack);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = null;
try {
result = response.body().string();
} catch (IOException e) {
deliverDataFailure(request, e, callBack);
}
deliverDataSuccess(result, callBack);
}
});
}
/**
* 分发失败的时候调用
*
* @param request
* @param e
* @param callBack
*/
private void deliverDataFailure(final Request request, final IOException e, final DataCallBack callBack) {
/**
* 在这里使用异步处理
*/
mHandler.post(new Runnable() {
@Override
public void run() {
if (callBack != null) {
callBack.requestFailure(request, e);
}
}
});
}
/**
* 分发成功的时候调用
*
* @param result
* @param callBack
*/
private void deliverDataSuccess(final String result, final DataCallBack callBack) {
/**
* 在这里使用异步线程处理
*/
mHandler.post(new Runnable() {
@Override
public void run() {
if (callBack != null) {
try {
callBack.requestSuccess(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
/**
* 数据回调接口
*/
public interface DataCallBack {
void requestFailure(Request request, IOException e);
void requestSuccess(String result) throws Exception;
}
}
最后一步就是进行MainActivity里面的代码工作
package com.example.administrator.okhttpdemo2.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.example.administrator.okhttpdemo2.R;
import com.example.administrator.okhttpdemo2.adapter.MyAdapter;
import com.example.administrator.okhttpdemo2.bean.Bean;
import com.example.administrator.okhttpdemo2.okhttp.OkHttp;
import com.google.gson.Gson;
import java.io.IOException;
import okhttp3.Request;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclrview;
private Bean bean;
private MyAdapter myAdapter;
private String path = "http://m.yunifang.com/yunifang/mobile/goods/getall?random=39986&encode=2092d7eb33e8ea0a7a2113f2d9886c90&category_id=17";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setOkHttp();
}
public void setRecyclerView() {
recyclrview = (RecyclerView) findViewById(R.id.recyclerview);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclrview.setLayoutManager(linearLayoutManager);
myAdapter = new MyAdapter(MainActivity.this, bean);
recyclrview.setAdapter(myAdapter);
}
public void setOkHttp() {
OkHttp.getAsync(path, new OkHttp.DataCallBack() {
@Override
public void requestFailure(Request request, IOException e) {
}
@Override
public void requestSuccess(String result) throws Exception {
Gson gson = new Gson();
bean = gson.fromJson(result, Bean.class);
setRecyclerView();
}
});
}
}
别忘记了在Androidapplication.xml里面添加联网权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
搞定,明白里面的过程其实很简单的,希望对其他新手同学有所帮助