做项目涉及到联网肯定是需要展示网络数据的,RecyclerView+Okhttp展示网络数据虽然很基础很简单,但想想还是写出来让初学者了解一下
1、首先先导入需要用到的依赖包
//recyclerview用到的依賴包
compile 'com.android.support:recyclerview-v7:25.1.1'
//网络请求依赖包
compile 'net.qiujuer.common:okhttp:3.0.0'
- 1
- 2
- 3
- 4
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
然后开始我们的代码工作
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 + '\'' +
'}';
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
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);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
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;
}
//-------------------------提交表单--------------------------
public static void postAsync(String url, Map<String, String> params, DataCallBack callBack) {
getInstance().inner_postAsync(url, params, callBack);
}
private void inner_postAsync(String url, Map<String, String> params, final DataCallBack callBack) {
RequestBody requestBody = null;
if (params == null) {
params = new HashMap<>();
}
/**
* 如果是3.0之前版本的,构建表单数据是下面的一句
*/
//FormEncodingBuilder builder = new FormEncodingBuilder();
/**
* 3.0之后版本
*/
FormBody.Builder builder = new FormBody.Builder();
/**
* 在这对添加的参数进行遍历,map遍历有四种方式,如果想要了解的可以网上查找
*/
for (Map.Entry<String, String> map : params.entrySet()) {
String key = map.getKey().toString();
String value = null;
/**
* 判断值是否是空的
*/
if (map.getValue() == null) {
value = "";
} else {
value = map.getValue();
}
/**
* 把key和value添加到formbody中
*/
builder.add(key, value);
}
requestBody = builder.build();
//结果返回
// 请求对象
final Request request = new Request.Builder().url(url).post(requestBody).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 = response.body().string();
deliverDataSuccess(result, callBack);
}
});
}
//-------------------------文件下载--------------------------
public static void downloadAsync(String url, String desDir, DataCallBack callBack) {
getInstance().inner_downloadAsync(url, desDir, callBack);
}
/**
* 下载文件的内部逻辑处理类
*
* @param url 下载地址
* @param desDir 目标地址
* @param callBack
*/
private void inner_downloadAsync(final String url, final String desDir, 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 {
/**
* 在这里进行文件的下载处理
*/
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
//文件名和目标地址
File file = new File(desDir, getFileName(url));
//把请求回来的response对象装换为字节流
inputStream = response.body().byteStream();
fileOutputStream = new FileOutputStream(file);
int len = 0;
byte[] bytes = new byte[2048];
//循环读取数据
while ((len = inputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, len);
}
//关闭文件输出流
fileOutputStream.flush();
//调用分发数据成功的方法
deliverDataSuccess(file.getAbsolutePath(), callBack);
} catch (IOException e) {
//如果失败,调用此方法
deliverDataFailure(request, e, callBack);
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
}
}
});
}
/**
* 根据文件url获取文件的路径名字
*
* @param url
* @return
*/
private String getFileName(String url) {
int separatorIndex = url.lastIndexOf("/");
String path = (separatorIndex < 0) ? url : url.substring(separatorIndex + 1, url.length());
return path;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
最后一步就是进行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();
}
});
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
别忘记了在Androidapplication.xml里面添加联网权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>