什么是网络编程?
网络编程从大的方面就是说对信息的发送接收。
通过操作相应API调度计算机资源硬件,并且利用管道(网线)进行数据交互的过程。
更为具体的涉及:网络模型、套接字、数据包
文件下载
接口:
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
| public void downFile(View view) { new Thread(new Runnable() { @Override public void run() { InputStream inputStream = null; FileOutputStream fileOutputStream = null; try { URL url = new URL(BASE_URL + "/download/11"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9"); httpURLConnection.setRequestProperty("Accept","*/*"); httpURLConnection.connect(); int responseCode = httpURLConnection.getResponseCode(); Log.d(TAG,"responseCode == > " + responseCode); if(responseCode == HttpURLConnection.HTTP_OK) { String headerField = httpURLConnection.getHeaderField("Content-disposition"); Log.d(TAG,"headerField == > " + headerField); String fileName = headerField.replace("attachment; filename=",""); Log.d(TAG,"fileName -- > " + fileName); File picFile = RequestTestActivity.this.getExternalFilesDir(Environment.DIRECTORY_PICTURES); if(!picFile.exists()) { picFile.mkdirs(); } File file = new File(picFile + File.separator + fileName); if(!file.exists()) { file.createNewFile(); } fileOutputStream = new FileOutputStream(file); inputStream = httpURLConnection.getInputStream(); byte[] buffer = new byte[1024]; int len; while((len = inputStream.read(buffer,0,buffer.length)) != -1) { fileOutputStream.write(buffer,0,len); } fileOutputStream.flush(); } } catch(Exception e) { e.printStackTrace(); } finally { IOUtils.ioClose(inputStream); IOUtils.ioClose(fileOutputStream); } } }).start(); }
|
Url携带参数
接口
get方法
/get/param
参数:
- keyword 关键字
- page 页码
- order 0 顺序 1 逆序
eg:
/get/param?keyword=你好&page=1&order=0
参数在Url上,我们可以直接对其进行拼接即可。
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
| private void startRequest(final Map<String,String> params,final String method,final String api) { new Thread(new Runnable() { @Override public void run() { BufferedReader bufferedReader = null; try { StringBuilder sb = new StringBuilder(); if(params != null && params.size() > 0) { sb.append("?"); Iterator<Map.Entry<String,String>> iterator = params.entrySet().iterator(); while(iterator.hasNext()) { Map.Entry<String,String> next = iterator.next(); sb.append(next.getKey()); sb.append("="); sb.append(next.getValue()); if(iterator.hasNext()) { sb.append("&"); } } Log.d(TAG,"sb result -- > " + sb.toString()); } String params = sb.toString(); URL url; if(params != null && params.length() > 0) { url = new URL(BASE_URL + api + params); } else { url = new URL(BASE_URL + api); } Log.d(TAG,"url -=-- > " + url.toString()); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod(method); httpURLConnection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9"); httpURLConnection.setRequestProperty("Accept","*/*"); httpURLConnection.connect(); int responseCode = httpURLConnection.getResponseCode(); if(responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = httpURLConnection.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String json = bufferedReader.readLine(); Log.d(TAG,"result -=-=- > " + json); } } catch(Exception e) { e.printStackTrace(); } finally { if(bufferedReader != null) { try { bufferedReader.close(); } catch(IOException e) { e.printStackTrace(); } } } } }).start(); }
|
测试代码
post/get
1 2 3 4 5 6 7 8 9 10 11 12
| public void getWithParams(View view) { Map<String,String> params = new HashMap<>(); params.put("keyword","这是我的关键字Keyword"); params.put("page","12"); params.put("order","0"); startRequest(params,"GET","/get/param"); } public void postWithParams(View view) { Map<String,String> params = new HashMap<>(); params.put("string","这是我提交的字符串"); startRequest(params,"POST","/post/string"); }
|
请求图片
网络权限和配置
1
| <uses-permission android:name="android.permission.INTERNET" />
|
新建xml文件夹,在其中新建network_security_config.xml
1 2 3 4 5 6 7 8 9 10
| <?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config> <domain includeSubdomains="true">example.com</domain> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">www.example.net</domain> <domain includeSubdomains="true">imgs.example.com</domain> </domain-config> </domain-config> </network-security-config>
|
请求图片内容
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
| public void requestImage(View view) { new Thread(new Runnable() { @Override public void run() { loadImage(); } }).start(); } private void loadImage() { try { URL url = new URL("https://imgs.example.com/group1/M00/00/05/rBsADV2rEz-AIzSoAABi-6nfiqs456.png"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(10000); httpURLConnection.connect(); InputStream is = httpURLConnection.getInputStream(); final Bitmap bitmap = BitmapFactory.decodeStream(is); runOnUiThread(new Runnable() { @Override public void run() { ImageView resultView = findViewById(R.id.image_result); resultView.setImageBitmap(bitmap); } }); } catch(Exception e) { e.printStackTrace(); } }
|
添加布局UI:一个Button和一个ImageView
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="requestImage" android:text="请求图片" />
<ImageView android:id="@+id/image_result" android:layout_width="match_parent" android:layout_height="match_parent" />
</LinearLayout>
|
这里面其实就是把图片流通过一个BitmapFactory来对流进行编码转成图片内容显示到控件上
post提交文本内容
接口地址
/post/comment
提交内容:
{
“articleId”:“234123”,
“commentContent”:“这是评论内容”
}
请求代码
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
| public void postRequest(View view) { new Thread(new Runnable() { @Override public void run() { OutputStream outputStream = null; InputStream inputStream = null; try { URL url = new URL(BASE_URL + "/post/comment"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setRequestProperty("Content-Type","application/json;charset=UTF-8"); httpURLConnection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9"); httpURLConnection.setRequestProperty("Accept","application/json, text/plain, */*"); CommentItem commentItem = new CommentItem("234134123","你的文章写得也太好了!"); Gson gson = new Gson(); String jsonStr = gson.toJson(commentItem); byte[] bytes = jsonStr.getBytes("UTF-8"); Log.d(TAG,"jsonStr length -- > " + bytes.length); httpURLConnection.setRequestProperty("Content-Length",String.valueOf(bytes.length)); httpURLConnection.connect(); outputStream = httpURLConnection.getOutputStream(); outputStream.write(bytes); outputStream.flush(); int responseCode = httpURLConnection.getResponseCode(); if(responseCode == HttpURLConnection.HTTP_OK) { inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); Log.d(TAG,"result -- > " + bufferedReader.readLine()); } } catch(Exception e) { e.printStackTrace(); } finally { IOUtils.ioClose(inputStream); IOUtils.ioClose(outputStream); } } }).start(); }
|
使用GsonFormat生成以下类:
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
| public class CommentItem { public CommentItem(String articleId,String commentContent) { this.articleId = articleId; this.commentContent = commentContent; }
private String articleId;
private String commentContent;
public String getArticleId() { return articleId; }
public void setArticleId(String articleId) { this.articleId = articleId; }
public String getCommentContent() { return commentContent; }
public void setCommentContent(String commentContent) { this.commentContent = commentContent; } }
|
运行结果
result – > {“success”:true,“code”:10000,“message”:“评论成功:你的文章写得也太好了!”,“data”:null}
单(多)文件上传
接口地址
方法:post
/file/upload
参数file
多文件上传
/files/upload
方法post 参数 files
抓包
用postMan上传一个文件进行抓包去拼接内容
代码实现
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
| private void uploadFile(File file,String fileKey,String fileName,String fileType,String BOUNDARY, OutputStream outputStream,boolean isLast) throws IOException { StringBuilder headerSbInfo = new StringBuilder(); headerSbInfo.append("--"); headerSbInfo.append(BOUNDARY); headerSbInfo.append("\r\n"); headerSbInfo.append("Content-Disposition: form-data; name=\"" + fileKey + "\"; filename=\"" + fileName + "\""); headerSbInfo.append("\r\n"); headerSbInfo.append("Content-Type: " + fileType); headerSbInfo.append("\r\n"); headerSbInfo.append("\r\n"); byte[] headerInfoBytes = headerSbInfo.toString().getBytes("UTF-8"); outputStream.write(headerInfoBytes); FileInputStream fos = new FileInputStream(file); BufferedInputStream bfi = new BufferedInputStream(fos); byte[] buffer = new byte[1024]; int len; while((len = bfi.read(buffer,0,buffer.length)) != -1) { outputStream.write(buffer,0,len); } StringBuilder footerSbInfo = new StringBuilder(); footerSbInfo.append("\r\n"); footerSbInfo.append("--"); footerSbInfo.append(BOUNDARY); if(isLast) { footerSbInfo.append("--"); footerSbInfo.append("\r\n"); } footerSbInfo.append("\r\n"); outputStream.write(footerSbInfo.toString().getBytes("UTF-8")); }
|
如果是多文件,我们只需要向body里多添加几个文件而已,格式一样的。
多文件上传调用
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
| OutputStream outputStream = null; try { File fileOne = new File("/storage/emulated/0/Download/1.jpg"); File fileTwo = new File("/storage/emulated/0/Download/rBsADV2rEz-AIzSoAABi-6nfiqs456.png"); File fileThree = new File("/storage/emulated/0/Download/2.jpg"); File fileFour = new File("/storage/emulated/0/Download/rBPLFV2A8POASi1aAAE-PgvGzOo723.jpg"); String fileKey = "files"; String fileType = "image/jpeg"; String BOUNDARY = "--------------------------954555323792164398227139"; URL url = new URL(BASE_URL + "/files/upload"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setRequestProperty("User-Agent", "Android/" + Build.VERSION.SDK_INT); httpURLConnection.setRequestProperty("Accept", "*/*"); httpURLConnection.setRequestProperty("Cache-Control", "no-cache"); httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); httpURLConnection.setRequestProperty("Connection", "keep-alive"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); httpURLConnection.connect(); outputStream = httpURLConnection.getOutputStream(); uploadFile(fileOne, fileKey, fileOne.getName(), fileType, BOUNDARY, outputStream, false); uploadFile(fileTwo, fileKey, fileTwo.getName(), fileType, BOUNDARY, outputStream, false); uploadFile(fileFour, fileKey, fileFour.getName(), fileType, BOUNDARY, outputStream, false); uploadFile(fileThree, fileKey, fileThree.getName(), fileType, BOUNDARY, outputStream, true); outputStream.flush(); int responseCode = httpURLConnection.getResponseCode(); Log.d(TAG, "responseCode -- > " + responseCode); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream)); String result = bf.readLine(); Log.d(TAG, "result -- > " + result); } } catch (Exception e) { e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
|
网络模型
关于java的网络编程,其实也没什么的,先说一下网络模型:OSI参考模型&TCP/IP参考模型
通常来讲,这个做开发的在这个传输层和网际层混,而应用层则是FTP/HTTP协议等,传输层就是这个TCP或者这个UDP,这个经常问到,面试的时候,说什么TCP和UDP的区别?
用户在这个应用层,我们在这个传输层和网际层。那么用户在操作数据时,是怎么样的过程呢?其实呀,数据先是从用户的应用层向下封装打包,再经过网络传输到接收端,接收端自下而上拆包,最后得到用户发送的数据。
大概是这样一个模式:A——->B——->B——–>A
网络通信的三要素
IP地址、端口、传输协议
首先是IP地址,其实它是一个网络设备标识,就像你家的地址一样,只是它用数字表示,或者说像门号一样。假如把酒店看成网络,那么每间房门号就是一个IP地址啦。看看它的英文:Internet Protocol,也就是网络协议的意思
说到IP,还要说到域名。因为IP纯数字,不好记忆,那么就用域名与之对应,这个呢DNS服务器来解决,域名与IP地址是映射关系。比如说,我们可以通过百度的IP地址来找到百度的网页:202.108.22.5 ,也可以通过www.baidu.com 来找到同样的页面,这里百度公司就是通过DNS服务器把域名www.baidu.com 解析到202.108.22.5,所以他们存在映射关系,这样的话,是不是网民记www.baidu.com 比记202.108.22.5这个好多啦!
还有就是特殊的IP地址:本地的IP地址,或者说是本地的回环地址:127.0.0.1,主机名是:localHost
你也可以打开这个DOC命令行,输入ipconfig查看网络配置。
知道了IP地址,那在JAVA中如何获取IP地址呢?其实很简单,在JAVA中IP地址对应的是:InetAddress类,在java.net包中放着,需要用的话,就不用客气地把它导入。
InetAddress这个类没有构造方法,所以不能实例化,但可以通过它本身的方法获取到对象,还记得单例设计模式也是这样吗?
其主要的方法:
由上面的表子可以看出,要获取到IP地址:首先要得到这个InetAddress,然后通过这个InetAddress对象的getHostAddress方法就可以获取到这个IP地址。
get:获得, Host:主机,Address:地址,也就是获取主机地址的意思
那么,如何获取这个InetAddress对象呢?从上面的表子也可以看出有好几个静态的方法呢!这里的前提是我们不知道这个IP地址要获取的是IP地址,所以我们用这个方法来获取InetAddress对象,然后通过getHostAddress()方法来获取IP地址!
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import java.net.*;
public class Demo{ public static void main(String[] args)throws Exception{ InetAddress ia = InetAddress.getByName("www.example.net"); String ip = ia.getHostAddress(); System.out.println("www.example.net的IP地址是:"+ip); } }
|
获取本地的IP地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import java.net.*;
public class Demo{ public static void main(String[] args)throws Exception{ InetAddress ia = InetAddress.getLocalHost(); String ip = ia.getHostAddress(); System.out.println("本机IP地址为:"+ip); } }
|
OK,搞定完IP之后就到这个端口
我们通过IP找到了主机对吧!但是这个主机上有很多的软件吧,他们都在运行,当我们发送一个数据到主机上,那这个数据到底是给哪个软件的呢,这样就出现问题了!解决这个问题当然是用端口啦,这个端口并不是物理端口,所谓的物理端口就是计算机硬件上的插口,这里的端口是逻辑端口,关于端口的几点说明如下:
1.端口的范围从0~65535(2的16次方减1)
2.系统保留端口为:0~1024
3.一些常用的端口,FTP:21,SMTP:25,HTTP:80
最后一个是:协议
顾名思义,协议就是一起商量好共同来往的相关规定,或者说是传输规则。传输协议、UDP和TCP协议。
重点:UDP和TCP传输协议的区别
解释一下上面的三次握手:
Client说:服务端哥,我想联你,在吗?—————>请求链接
Server说:客户端弟,我在的,你联过来吧!———->服务端确认链接
Client说:那好,我真的联上来啦!——————–>客户端确认链接
计算机与计算机之间的通信步骤:
1、通过IP地址找到主机;2、通过端口找到接收的程序;3、通过协议发送数据。
传输协议:UDP
UDP:不面向链接、不可靠、速度快,一般用于聊天、视频、通话之类的一次性数据传输,不需要精确数据或者永久数据的情况下使用。
在说这两个传输协议之前,要提到的是Socket,Socket大概是插座的意思,那么这里呢可以理解为网线插口,也就是一个端点。
现在有两台主机之间要传输数据,所以呢,首先要有码头,那我们看看如何创建码头。
UDP的码头是DatagramSocket,它可以接受和发送数据"包"。一般发送的话要指定IP地址,还有端口,而接受端可以不指定IP,但要监视端口。下面是DatagramSocket的常用构造方法:
DatagramSocket() 构造数据报套接字并将其绑定到本地主机上任何可用的端口。 DatagramSocket(int port) 创建数据报套接字并将其绑定到本地主机上的指定端口。 DatagramSocket(int port, InetAddress laddr) 创建数据报套接字,将其绑定到指定的本地地址。
第一个一般用于接收端,第二个用于发送端。
常用方法:
主要用到的还是接收和发送,另外就是获取IP地址,我们还需要一个类DatagramPacket.这个类是用于把数据打包的。常用的构造方法列一下:
有了码头,有了数据包,接下来就是形象生动地说说步骤:
发送端的操作:
1.建立Socket服务,随便你指不指定端口,不指定系统分配。另外就是端口顺延,如果这次端口是1555,下次就是1556,因为当你运行完程序后,不能保证端口也释放了,为了防止出错,所以就这样顺延下去咯!
2.将要发送的数据封装到包中,准备发送.
3.发送数据,通过send方法发送出去.
4.最后,不要忘记关闭资源!
发送端代码:
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
|
import java.net.*;
public class Demo{ public static void main(String[] args)throws Exception{ DatagramSocket ds = new DatagramSocket(); String data = "example:example.com"; byte[] buf = data.getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("PresidentsPC"), 10000); ds.send(dp); ds.close(); } }
|
接受端的步骤
1.建立服务
2.定义一个缓冲区来接受数据
3.调用DatagramPacket中的各种方法来达到我们的需求
4.关闭资源
接收端的代码:
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
|
import java.net.*;
public class Server{ public static void main(String[] args)throws Exception{ DatagramSocket ds = new DatagramSocket(10000); byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf,buf.length); ds.receive(dp); String ip = dp.getAddress().getHostAddress(); System.out.println("来自"+ip+"的数据是"); System.out.println(new String(buf,0,dp.getLength())); ds.close(); } }
|
eg:先是发送键盘录入的程序,下面分析一下:
1.首先,要建立服务
2.获取资源,从控制台中获取,这个可以用高效的方法BufferedReader,读一行。
3.把资源打成包发送出去
4.关闭资源。
PS:这次我们进行异常处理
第一个是发送端(客户端):
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
|
import java.net.*; import java.io.*;
public class Demo{ public static void main(String[] args){ DatagramSocket ds = null; BufferedReader br = null; try{ if(ds==null) ds = new DatagramSocket(); if(br==null) br = new BufferedReader(new InputStreamReader(System.in)); byte[] buf = br.readLine().getBytes(); DatagramPacket dp = new DatagramPacket(buf,buf.length, InetAddress.getByName("PresidentsPC"), 10004); ds.send(dp); }catch(Exception e){ System.out.println("数据发送失败"); }finally{ ds.close(); if(br!=null) try{ br.close(); }catch(Exception e){ System.out.println("流关不了,坏了!"); } } } }
|
接下来是接收端(服务端):
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
|
import java.net.*;
public class Server{ public static void main(String[] args){ DatagramSocket ds = null; DatagramPacket dp = null; try{ if(ds==null) ds = new DatagramSocket(10004); byte[] buf = new byte[1024]; if(dp==null) dp = new DatagramPacket(buf,buf.length); ds.receive(dp); String ip = dp.getAddress().getHostName(); System.out.println(ip+" :"+new String(buf, 0, dp.getLength())); }catch(Exception e){ System.out.println("哎呀,妈呀,出问题了!"); }finally{ ds.close(); } } }
|
接下来,我们要写一个多线程的,不断地接收数据
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
| import java.io.*; import java.net.*;
public class ChatDemo {
public static void main(String[] args) { new Thread(new ChatThread()).start();
DatagramSocket ds = null; BufferedReader br = null; try { if (ds == null) ds = new DatagramSocket(); if (br == null) br = new BufferedReader(new InputStreamReader(System.in)); String line = null; while ((line = br.readLine()) != null) { byte[] buf = line.getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("PresidentsPC"), 10000); ds.send(dp); } } catch (Exception e) { System.out.println(e.toString()); } finally {
ds.close(); if (br != null) try { br.close(); } catch (Exception e) { System.out.println(e.toString()); } } } }
class ChatThread implements Runnable { public void run(){ while(true){ DatagramSocket ds = null; try { ds = new DatagramSocket(10000); byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf,buf.length); ds.receive(dp); String ip = dp.getAddress().getHostAddress(); System.out.println(ip+" :"+new String(buf,0,dp.getLength())); } catch (Exception e) { System.out.println(e.toString()); }finally{ ds.close(); } } } }
|
传输协议:TCP
TCP第一步也是创建服务,但是这服务稍微有点不同:客户端的服务是Sokcet,服务端的服务是ServerSocket。无论是UDP,还是TCP,只要多写几次就熟悉了。
先看看Socket的常用的构造方法和普通方法
客户端的步骤:
1.建立服务Socket
2.获取输出流,把数据变成字节数组,通过输出流发送给服务端。
3.关闭输出流,获取输入流,获取反馈信息
4.关闭资源
代码体现:
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
| import java.io.*; import java.net.*;
public class Client {
public static void main(String[] args) {
Socket s = null; try { if (s == null) s = new Socket("PresidentsPC", 13000); byte[] buf = "example".getBytes(); OutputStream out = s.getOutputStream(); out.write(buf); s.shutdownOutput(); InputStream in = s.getInputStream(); byte[] buffer = new byte[1024]; int len = in.read(buffer); System.out.println(new String(buffer,0,len)); } catch (Exception e) { System.out.println(e.toString()); }finally{ if(s!=null){ try{ s.close(); }catch(Exception e) { System.out.println(e.toString());} } }
} }
|
服务端又如何呢,其实还是大同小异的,要是IO知识掌握了,一点问题都没有.
步骤如下:
1.建立服务ServerSocket服务,然后用ServerSocket的accept()方法得到Socket服务
2.获取输入流,然后可以得到数据
3.对读到的数据进行处理,该干嘛干嘛去
4.反馈信息给客户端
5.各种关闭资源
代码:
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
| import java.io.*; import java.net.*;
public class Server {
public static void main(String[] args)throws Exception {
ServerSocket ss = new ServerSocket(13000); Socket s = ss.accept(); String ip = s.getInetAddress().getHostAddress(); System.out.println(ip+"-----connected"); InputStream in = s.getInputStream(); byte[] buf = new byte[1024]; int len = 0; while((len = in.read(buf))!=-1){ System.out.println(new String(buf,0,len)); } s.shutdownInput(); OutputStream out = s.getOutputStream(); out.write("服务端收到啦".getBytes()); s.close(); ss.close(); } }
|
这个例子也搞定了,接下来就可以更深入发学习TCP其他场景的应用了。比如说,上传图片并发上传图片,还要追求高效,这可以采用缓冲技术。
网络传输应用
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
| import java.io.*; import java.net.*;
class ServerForAll {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(19000); while (true) { Socket s = ss.accept(); new Thread(new ServerThread(s)).start(); }
}
}
class ServerThread implements Runnable { private Socket s;
public ServerThread(Socket s) { this.s = s; }
public void run() {
InputStream in = null; OutputStream out = null; OutputStream outPutFile = null; String ip = s.getInetAddress().getHostAddress(); System.out.println(ip+"---connected"); int count = 0; try { in = s.getInputStream(); out = s.getOutputStream(); File file = new File("Data.jpg"); if (file.exists()) file = new File("Data" + "(" + (count++) + ")"); outPutFile = new FileOutputStream(file);
byte[] buf = new byte[1024]; int len = 0; while ((len = in.read(buf)) != -1) { outPutFile.write(buf, 0, len); } out.write("上传成功!".getBytes());
s.close(); outPutFile.close(); } catch (Exception e) { System.out.println(e.toString()); }
} }
class Client {
public static void main(String[] args) { if (args.length == 0) { System.out.println("请传入jpg图片的路径参数"); return; } if (!args[0].endsWith(".jpg")) { System.out.println("图片格式不正确"); return; }
File file = new File(args[0]); if (!file.exists()) { System.out.println("文件不存在"); return; } Socket s = null; InputStream in = null; try { s = new Socket("PresidentsPC", 19000); in = new FileInputStream(file); OutputStream out = s.getOutputStream(); byte[] buf = new byte[1024]; int len = 0; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } s.shutdownOutput();
InputStream input = s.getInputStream(); byte[] buffer = new byte[1024]; int length = input.read(buffer);
System.out.println(new String(buffer, 0, length)); } catch (Exception e) { System.out.println(e.toString()); } finally { if (s != null) { try { s.close(); } catch (Exception e) { System.out.println(e.toString()); } if (in != null) try { in.close(); } catch (Exception e) { System.out.println(e.toString()); } }
}
} }
|
http协议
什么是http协议?
http是什么意思?HyperText Transfer Protocol 翻译过来就是超文本传输协议
协议就是约定的意思,内容是Http相关的格式
http协议是基于TCP/IP协议之上的应用层协议
http工作流程
客户端发起一个请求,然后服务器处理请求,做出响应。
这个流程是一定的,换句话说,请求一定是客户端请求,响应一定是服务端响应。不可以是服务端向客户端发起请求,也不可以是客户端做出响应。
这种情况是有的,但是当发起请求那一刻,你就要把它看成客户端了。
这是充要条件
客户端=>发起请求,发起请求=>客户端
响应结果=>服务端。服务端=>响应结果
http不保存状态
当一个请求,一个响应完成时,那么一个http的请求就完成了。当然也有特殊情况,就是无网了,你发不起请求。然后服务器处理超时了,你也得不到结果。
http是无状态协议,什么意思呢?也就是不保存状态。一次请求完成后,下一次再发起请求,又是新的。该设置什么内容设置什么内容,要给什么参数给什么参数。
http无状态,那怎么知道这个请求用户已经登录了呢?登录状态保存在哪里呢?所以就引入了cookies这个东西了。在cookies里可以保存一个令牌,每次请求的时候,把这个令牌作为参数给服务器校验,这样子就可以知道这个用户的登录状态啦。
http的8种请求方式
- get
- post
- put
- delete
- head
- trace
- options
- connect
作为Android开发人员知道前面四个就够了。
通常情况下:
get:用于请求数据 post:用于提交数据 put:用于更新数据 delete:用于删除数据
同一个接口,不同的请求方式作用就不一样了
请求文章的CRUD接口
- 请求:/article/{articleId} 请求方式:get请求
- 删除:/article/{articleId} 请求方式:deleted请求
- 更新:/article/{articleId} 请求方式:put请求
- 提交(发表文章):/article/ 请求方式:post请求
http响应码
响应码:说明是服务器给客户端结果码
相信大家在浏览某些网站时应该见过404吧,那么404是什么意思呢?
404也就是4xx,4开头的表示服务器无法请求
而5开头的,比如说500,服务器崩溃了指的是服务器的问题
http请求格式
当我们使用http向服务器发起一个请求时,它的格式是怎么样子的呢?
首先这个请求是包含了:
- 请求行
- 请求头
- 空行
- 请求体
比如说我们用fiddler抓个get请求看看
get请求:
post或者put请求:
请求行
请求头
- Host 要访问的主机
- Connection 连接保持,http1.1有,1.0没有这个。keep-alive可以提高效率,在一定时间里不断开tcp连接,进行下一个请求
- Content-Length 内容长度,指的是后面请求正文的数据长度
- Accept 响应内容格式,也就是返回内容格式
- Origin 这个是源(不是标准http里的内容,因为我访问用的是chrome,是chrome加的)
- User-Agent 用户客户端相关的信息,比如说浏览器呀,操作系统信息之类的
- Content-Type 提交的内容类型,我提及的是json,编码是utf-8
- Referer 来源,访问入口,比如说你在搜索引擎百度里搜索,进入到网站,那么这个来源就是百度了
- Accept-Encoding 响应内容的编码格式
- Accept-Language 响应内容的语言
- Cookie cookies
请求体
{“state”:“0”,“msg”:“审核通过.”}
请求正文内容
http响应格式
使用Java的API发起网络请求
前面我们了解了Http协议,Android应用是用java编写的,到framework层还是用java编写的,而Android的网络请求,其实是对java的网络请求进行了封装。
在android6.0以前,有apache的HttpClient,使用简单
但是
所以,如果同学们以后打开别人的项目时报错,就是没有httpclient的时候就需要加上这个了。
1 2 3
| android { useLibrary 'org.apache.http.legacy' }
|
推荐使用HttpURLConnection
官方文档
我们先看看java是如何进行网络请求的
代码:
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
| private static void loadData(String baseUrl, Map<String, String> params) { try { StringBuilder sb = new StringBuilder(baseUrl); if (params.size() > 0) { sb.append("?"); Set<Map.Entry<String, String>> entries = params.entrySet(); for (Map.Entry<String, String> entry : entries) { sb.append(entry.getKey()); sb.append("="); sb.append(entry.getValue()); } } String resultUrl = sb.toString(); URL url = new URL(resultUrl); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setConnectTimeout(1000); urlConnection.setRequestProperty("accept", "*/*"); urlConnection.setRequestProperty("connection", "keep-alive"); urlConnection.setRequestProperty("Accept-Language", "zh-CN,zh"); urlConnection.connect(); Map<String, List<String>> headerFields = urlConnection.getHeaderFields(); Set<Map.Entry<String, List<String>>> entries = headerFields.entrySet(); for (Map.Entry<String, List<String>> entry : entries) { System.out.println(entry.getKey() + " === " + entry.getValue()); } InputStream inputStream = urlConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } }
|
调用:
1 2 3 4
| public static void main(String[] args) { Map<String, String> params = new HashMap<>(); loadData("https://www.example.net", params); }
|
这样我们就把网页的内容获取到了
获取一个json数据:
1 2 3 4 5 6 7
| public static void main(String[] args) { Map<String, String> params = new HashMap<>(); loadData("https://www.example.net/content/content/moment/list/1153952789488054272/1", params); }
|
将获取到的结果复制出来格式化一下,这样数据就回来了
我们先搞定请求先,然后转到Android上,把数据请求回来,显示在UI上。
Android上使用java的API请求数据
网络配置
1
| <uses-permission android:name="android.permission.INTERNET" />
|
请求的代码
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
| import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View;
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
public void startRequest(View view) { new Thread(new Runnable() { @Override public void run() { loadData(); } }).start(); }
private void loadData() { try { URL url = new URL("https://www.example.net/content/content/moment/list/1153952789488054272/1"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); int responseCode = httpURLConnection.getResponseCode(); if(responseCode == 200) { httpURLConnection.setConnectTimeout(1000); InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8")); String line; while((line = bufferedReader.readLine()) != null) { Log.d(TAG,line); } bufferedReader.close(); } } catch(Exception e) { e.printStackTrace(); } }
}
|
将请求回来的数据用Gson将结果格式化,用列表显示不在UI上。
MainActivity:
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
| import android.annotation.SuppressLint; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity"; public static final int WHAT_LOADER_RESULT = 1; private static Handler mHandler; private RecyclerView mResultList; private ResultAdapter mResultAdapter;
@SuppressLint("HandlerLeak") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch(msg.what) { case WHAT_LOADER_RESULT: MomentItem result = (MomentItem) msg.obj; refreshResultList(result); break; } } }; initView(); }
private void initView() { mResultList = findViewById(R.id.result_list); mResultList.setLayoutManager(new LinearLayoutManager(this)); mResultAdapter = new ResultAdapter(); mResultList.setAdapter(mResultAdapter); }
private void refreshResultList(MomentItem result) { Log.d(TAG,"refreshResultList -- "); mResultAdapter.setData(result); }
public void startRequest(View view) { new Thread(new Runnable() { @Override public void run() { loadData(); } }).start(); }
private void loadData() { try { URL url = new URL("https://网站"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(1000); int responseCode = httpURLConnection.getResponseCode(); if(responseCode == 200) { InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8")); String line = bufferedReader.readLine(); Log.d(TAG,"line -- > " + line); bufferedReader.close(); Message message = mHandler.obtainMessage(); message.what = WHAT_LOADER_RESULT; Gson gson = new Gson(); message.obj = gson.fromJson(line,MomentItem.class); mHandler.sendMessage(message); } } catch(Exception e) { e.printStackTrace(); } }
}
|
bean类:通过GsonFormat自动生成
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 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
| import java.util.List;
public class MomentItem {
private boolean success; private int code; private String message; private DataBean data;
public boolean isSuccess() { return success; }
public void setSuccess(boolean success) { this.success = success; }
public int getCode() { return code; }
public void setCode(int code) { this.code = code; }
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
public DataBean getData() { return data; }
public void setData(DataBean data) { this.data = data; }
public static class DataBean {
private PageableBean pageable; private int totalPages; private int totalElements; private boolean last; private boolean first; private SortBeanX sort; private int numberOfElements; private int size; private int number; private List<ContentBean> content;
public PageableBean getPageable() { return pageable; }
public void setPageable(PageableBean pageable) { this.pageable = pageable; }
public int getTotalPages() { return totalPages; }
public void setTotalPages(int totalPages) { this.totalPages = totalPages; }
public int getTotalElements() { return totalElements; }
public void setTotalElements(int totalElements) { this.totalElements = totalElements; }
public boolean isLast() { return last; }
public void setLast(boolean last) { this.last = last; }
public boolean isFirst() { return first; }
public void setFirst(boolean first) { this.first = first; }
public SortBeanX getSort() { return sort; }
public void setSort(SortBeanX sort) { this.sort = sort; }
public int getNumberOfElements() { return numberOfElements; }
public void setNumberOfElements(int numberOfElements) { this.numberOfElements = numberOfElements; }
public int getSize() { return size; }
public void setSize(int size) { this.size = size; }
public int getNumber() { return number; }
public void setNumber(int number) { this.number = number; }
public List<ContentBean> getContent() { return content; }
public void setContent(List<ContentBean> content) { this.content = content; }
public static class PageableBean {
private SortBean sort; private int pageSize; private int pageNumber; private int offset; private boolean unpaged; private boolean paged;
public SortBean getSort() { return sort; }
public void setSort(SortBean sort) { this.sort = sort; }
public int getPageSize() { return pageSize; }
public void setPageSize(int pageSize) { this.pageSize = pageSize; }
public int getPageNumber() { return pageNumber; }
public void setPageNumber(int pageNumber) { this.pageNumber = pageNumber; }
public int getOffset() { return offset; }
public void setOffset(int offset) { this.offset = offset; }
public boolean isUnpaged() { return unpaged; }
public void setUnpaged(boolean unpaged) { this.unpaged = unpaged; }
public boolean isPaged() { return paged; }
public void setPaged(boolean paged) { this.paged = paged; }
public static class SortBean {
private boolean unsorted; private boolean sorted;
public boolean isUnsorted() { return unsorted; }
public void setUnsorted(boolean unsorted) { this.unsorted = unsorted; }
public boolean isSorted() { return sorted; }
public void setSorted(boolean sorted) { this.sorted = sorted; } } }
public static class SortBeanX {
private boolean unsorted; private boolean sorted;
public boolean isUnsorted() { return unsorted; }
public void setUnsorted(boolean unsorted) { this.unsorted = unsorted; }
public boolean isSorted() { return sorted; }
public void setSorted(boolean sorted) { this.sorted = sorted; } }
public static class ContentBean {
private String _id; private String content; private int type; private int thumbUpCount; private int commentCount; private String url; private String covers; private Object subContent; private Object subUserName; private Object subUserAvatar; private Object subUserId; private String userName; private String userId; private String userAvatar; private String publishTime; private String subTitle; private String position; private String company; private boolean hasThumbUp; private List<?> thumbList; private List<String> images;
public String get_id() { return _id; }
public void set_id(String _id) { this._id = _id; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
public int getType() { return type; }
public void setType(int type) { this.type = type; }
public int getThumbUpCount() { return thumbUpCount; }
public void setThumbUpCount(int thumbUpCount) { this.thumbUpCount = thumbUpCount; }
public int getCommentCount() { return commentCount; }
public void setCommentCount(int commentCount) { this.commentCount = commentCount; }
public String getUrl() { return url; }
public void setUrl(String url) { this.url = url; }
public String getCovers() { return covers; }
public void setCovers(String covers) { this.covers = covers; }
public Object getSubContent() { return subContent; }
public void setSubContent(Object subContent) { this.subContent = subContent; }
public Object getSubUserName() { return subUserName; }
public void setSubUserName(Object subUserName) { this.subUserName = subUserName; }
public Object getSubUserAvatar() { return subUserAvatar; }
public void setSubUserAvatar(Object subUserAvatar) { this.subUserAvatar = subUserAvatar; }
public Object getSubUserId() { return subUserId; }
public void setSubUserId(Object subUserId) { this.subUserId = subUserId; }
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public String getUserId() { return userId; }
public void setUserId(String userId) { this.userId = userId; }
public String getUserAvatar() { return userAvatar; }
public void setUserAvatar(String userAvatar) { this.userAvatar = userAvatar; }
public String getPublishTime() { return publishTime; }
public void setPublishTime(String publishTime) { this.publishTime = publishTime; }
public String getSubTitle() { return subTitle; }
public void setSubTitle(String subTitle) { this.subTitle = subTitle; }
public String getPosition() { return position; }
public void setPosition(String position) { this.position = position; }
public String getCompany() { return company; }
public void setCompany(String company) { this.company = company; }
public boolean isHasThumbUp() { return hasThumbUp; }
public void setHasThumbUp(boolean hasThumbUp) { this.hasThumbUp = hasThumbUp; }
public List<?> getThumbList() { return thumbList; }
public void setThumbList(List<?> thumbList) { this.thumbList = thumbList; }
public List<String> getImages() { return images; }
public void setImages(List<String> images) { this.images = images; } } } }
|
适配器的代码:
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
| import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList; import java.util.List;
import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView;
public class ResultAdapter extends RecyclerView.Adapter<ResultAdapter.InnerAdapter> {
private List<MomentItem.DataBean.ContentBean> mData = new ArrayList<>();
@NonNull @Override public InnerAdapter onCreateViewHolder(@NonNull ViewGroup parent,int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_moment,parent,false); return new InnerAdapter(itemView); }
@Override public void onBindViewHolder(@NonNull InnerAdapter holder,int position) { View itemView = holder.itemView; TextView momentTitle = itemView.findViewById(R.id.moment_title); ImageView avatar = itemView.findViewById(R.id.user_avatar); TextView userInfo = itemView.findViewById(R.id.user_info); TextView userName = itemView.findViewById(R.id.user_name); MomentItem.DataBean.ContentBean contentBean = mData.get(position); String subTitle = contentBean.getSubTitle(); if(!TextUtils.isEmpty(subTitle)) { momentTitle.setText(subTitle); } else { momentTitle.setText(contentBean.getContent()); } userName.setText(contentBean.getUserName()); userInfo.setText(contentBean.getPosition() + "@" + contentBean.getCompany()); Glide.with(itemView.getContext()).load(contentBean.getUserAvatar()).into(avatar); }
@Override public int getItemCount() { return mData.size(); }
public void setData(MomentItem result) { mData.clear(); mData.addAll(result.getData().getContent()); notifyDataSetChanged(); }
public class InnerAdapter extends RecyclerView.ViewHolder { public InnerAdapter(@NonNull View itemView) { super(itemView); } } }
|
item的布局:
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
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<ImageView android:id="@+id/user_avatar" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/ic_launcher" />
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:orientation="vertical">
<TextView android:id="@+id/user_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="2dp" android:text="用户名称" android:textColor="#0086FF" android:textSize="18sp" />
<TextView android:id="@+id/user_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="2dp" android:text="用户信息" />
</LinearLayout> </LinearLayout> </LinearLayout>
|
Java8api文档