Okio源码学习分析,2024年最新android开发实例大全源代码

// - Avoid short shared segments. These are bad for performance because they are readonly and

// may lead to long chains of short segments.

// To balance these goals we only share segments when the copy will be large.

if (byteCount >= SHARE_MINIMUM) {

prefix = new Segment(this);

} else {

prefix = SegmentPool.take();

System.arraycopy(data, pos, prefix.data, 0, byteCount);

}

prefix.limit = prefix.pos + byteCount;

pos += byteCount;

prev.push(prefix);

return prefix;

}

/**

  • Call this when the tail and its predecessor may both be less than half

  • full. This will copy data so that segments can be recycled.

*/

public void compact() {

if (prev == this) throw new IllegalStateException();

if (!prev.owner) return; // Cannot compact: prev isn’t writable.

int byteCount = limit - pos;

int availableByteCount = SIZE - prev.limit + (prev.shared ? 0 : prev.pos);

if (byteCount > availableByteCount) return; // Cannot compact: not enough writable space.

writeTo(prev, byteCount);

pop();

SegmentPool.recycle(this);

}

/** Moves {@code byteCount} bytes from this segment to {@code sink}. */

public void writeTo(Segment sink, int byteCount) {

if (!sink.owner) throw new IllegalArgumentException();

if (sink.limit + byteCount > SIZE) {

// We can’t fit byteCount bytes at the sink’s current position. Shift sink first.

if (sink.shared) throw new IllegalArgumentException();

if (sink.limit + byteCount - sink.pos > SIZE) throw new IllegalArgumentException();

System.arraycopy(sink.data, sink.pos, sink.data, 0, sink.limit - sink.pos);

sink.limit -= sink.pos;

sink.pos = 0;

}

System.arraycopy(data, pos, sink.data, sink.limit, byteCount);

sink.limit += byteCount;

pos += byteCount;

}

}

首先,Segment中有几个成员变量:Segment.SIZE这个值是8192,也就是8kb, 是一个Segment对象能处理的数据的大小,byte[] data这个就是真正的存储数据的字节数组,pos这个是读取数据的起始位置,limit是写数据的起始位置,shared表示当前Segment的字节数组data是否可以共享的,owner表示当前Segment是否是data对象的持有者(只有data对象的持有者才能对data进行修改), 只有share为false即表示owner为true是当前的持有者。这里有个概念就是share “共享”,Segment中的data数组是可以在Buffer和ByteString对象之间共享的,怎么来确认这个共享呢,我们看到Segment对象有三个构造函数,其中有参的构造函数:

Segment(Segment shareFrom) {

this(shareFrom.data, shareFrom.pos, shareFrom.limit);

shareFrom.shared = true;

}

Segment(byte[] data, int pos, int limit) {

this.data = data;

this.pos = pos;

this.limit = limit;

this.owner = false;

this.shared = true;

}

也就是通过外部传递Segment对象和data数组的方式构造出来的Segment就是共享的,而默认的构造函数:

Segment() {

this.data = new byte[SIZE];

this.owner = true;

this.shared = false;

}

这样出来的就是不共享的Segment对象。

继续,nextprev就是分别代表后继节点和前驱节点的对象,并以此来形成双向链表,那么怎么形成的双向链表呢?就是通过调用push方法,具体先放着,后面看Buffer的时候再细看。Segment中的主要方法为pop()push()split()compact(),其中pop()方法的作用是将当前的Segment对象从双向链表中移除,并返回链表中的下一个结点作为头结点,而push()方法的作用则是在双向链表中当前结点的后面插入一个新的Segment结点对象,并移动next指向新插入的结点。后面两个方法主要是对Segment进行分割和合并,

提到Segment,还有一个与之相关的类SegmentPool类:

/**

  • A collection of unused segments, necessary to avoid GC churn and zero-fill.

  • This pool is a thread-safe static singleton.

*/

final class SegmentPool {

/** The maximum number of bytes to pool. */

// TODO: Is 64 KiB a good maximum size? Do we ever have that many idle segments?

static final long MAX_SIZE = 64 * 1024; // 64 KiB.

/** Singly-linked list of segments. */

static @Nullable Segment next;

/** Total bytes in this pool. */

static long byteCount;

private SegmentPool() {

}

static Segment take() {

synchronized (SegmentPool.class) {

if (next != null) {

Segment result = next;

next = result.next;

result.next = null;

byteCount -= Segment.SIZE;

return result;

}

}

return new Segment(); // Pool is empty. Don’t zero-fill while holding a lock.

}

static void recycle(Segment segment) {

if (segment.next != null || segment.prev != null) throw new IllegalArgumentException();

if (segment.shared) return; // This segment cannot be recycled.

synchronized (SegmentPool.class) {

if (byteCount + Segment.SIZE > MAX_SIZE) return; // Pool is full.

byteCount += Segment.SIZE;

segment.next = next;

segment.pos = segment.limit = 0;

next = segment;

}

}

}

SegmentPool可以理解为一个缓存Segment的池,它只有两个方法,一个take(),一个recycle(),在SegmentPool中维护的是一个Segment 的单链表,并且它的最大值为MAX_SIZE = 64 * 1024也就是64kb8个Segment的长度,next就是单链表中的头结点。

take()方法的作用是取出单链表的头结点Segment对象,然后将取出的对象与链表断开并将链表往后移动一个单位,如果是第一次调用take, next为null, 则会直接new一个Segment对象返回,并且这里创建的Segment是不共享的。

recycle()方法的作用则是回收一个Segment对象,被回收的Segment对象将会被插入到SegmentPool中的单链表的头部,以便后面继续复用,并且这里源码我们也可以看到如果是shared的对象是不处理的,如果是第一次调用recycle()方法则链表会由空变为拥有一个节点的链表, 每次回收就会插入一个到表头,直到超过最大容量。

Buffer

如果你只看Segment的话还是很难理解整个数据的读写流程,因为你只知道它是能够形成一个链表的东西,但是当你看完Buffer之后完整的流程就会清晰多了。

Buffer类是Okio中最核心并且最丰富的类了,前面分析发现最终的Source和Sink实现对象中,都是通过该类完成读写操作,而Buffer类同时实现了BufferedSourceBufferedSink接口,因此Buffer具备Okio中的读和写的所有方法,所以这个类的方法超多!我们只找一个读和写的方法来看一下实现好了。

byte[]操作:

@Override

public Buffer write(byte[] source, int offset, int byteCount) {

if (source == null) throw new IllegalArgumentException(“source == null”);

// 检测参数的合法性

checkOffsetAndCount(source.length, offset, byteCount);

// 计算 source 要写入的最后一个字节的 index 值

int limit = offset + byteCount;

while (offset < limit) {

// 获取循环链表尾部的一个 Segment

Segment tail = writableSegment(1);

// 计算最多可写入的字节

int toCopy = Math.min(limit - offset, Segment.SIZE - tail.limit);

// 把 source 复制到 data 中

System.arraycopy(source, offset, tail.data, tail.limit, toCopy);

// 调整写入的起始位置

offset += toCopy;

// 调整尾部Segment 的 limit 位置

tail.limit += toCopy;

}

// 调整 Buffer 的 size 大小

size += byteCount;

return this;

}

写操作内部是调用System.arraycopy进行字节数组的复制,这里是写到tail对象,也就是循环链表的链尾Segment对象当中,而且这里会不断循环的获取链尾Segment对象进行写入。

看一下获取链尾的方法:

/**

  • Returns a tail segment that we can write at least {@code minimumCapacity}

  • bytes to, creating it if necessary.

*/

Segment writableSegment(int minimumCapacity) {

if (minimumCapacity < 1 || minimumCapacity > Segment.SIZE) throw new IllegalArgumentException();

// 如果链表的头指针为null,就会SegmentPool中取出一个

if (head == null) {

head = SegmentPool.take(); // Acquire a first segment.

return head.next = head.prev = head;

}

// 获取前驱结点,也就是尾部结点

Segment tail = head.prev;

// 如果能写的字节数限制超过了8192,或者不是拥有者

if (tail.limit + minimumCapacity > Segment.SIZE || !tail.owner) {

// 从SegmentPool中获取一个Segment,插入到循环双链表当前结点的后面

tail = tail.push(SegmentPool.take()); // Append a new empty segment to fill up.

}

return tail;

}

这里有个head对象,就是Segment链表的头结点的引用,这个方法中可以看到如果写的时候头结点head为空,则会调用 SegmentPool.take() 方法从Segment池中获取一个 Segment缓存对象,并以此形成一个双向链表的初始节点:

if (head == null) {

head = SegmentPool.take(); // Acquire a first segment.

return head.next = head.prev = head;

}

这时Segment中会形成下面这样的初始链表:

这时头结点和尾节点其实是同一个节点,然后取得head.prev也就是tail尾节点返回,但是如果此时tail能写的字节数限制超过了8k或者尾节点不是data的拥有者,就会调用tail.push(SegmentPool.take());也就是再调用一次SegmentPool.take()取到Segment池中下一个Segment. 通过tail. push() 方法插入到循环链表的尾部。这时Segment中的链表会变成下面这样:

此时插入的节点会作为新的tail节点返回,下一次获取尾节点的时候就会取到它,每当tail进行push一次,就会将新push的节点作为新的尾节点:

byte[]操作:

@Override

public int read(byte[] sink, int offset, int byteCount) {

checkOffsetAndCount(sink.length, offset, byteCount);

//取到Segment循环链表的表头

Segment s = head;

if (s == null) return -1;

// 计算最多可写入的字节

int toCopy = Math.min(byteCount, s.limit - s.pos);

//将数据拷贝到链头的data字节数组当中

System.arraycopy(s.data, s.pos, sink, offset, toCopy);

//调整链头的data数组的起始postion和Buffer的size

s.pos += toCopy;

size -= toCopy;

//pos等于limit的时候,从循环链表中移除该Segment并从SegmentPool中回收复用

if (s.pos == s.limit) {

head = s.pop();//移除的同时返回下一个Segment作为表头

SegmentPool.recycle(s);

}

return toCopy;

}

读操作内部也是调用System.arraycopy进行字节数组的复制,这里是直接对head头结点进行读取,也就是说Buffer在每次读数据的时候都是从链表的头部进行读取的,如果读取的头结点的pos等于limit, 这里就会调用s.pop()将头节点从链表中删除,并返回下一个节点作为新的头结点引用,然后将删除的节点通过SegmentPool.recycle(s)进行回收复用。这时链表中的变化如下:

以上是读写字节数据的过程,读取其它数据类型如int、long、String,过程类似,所以简单的概括Buffer中读的过程就是不断取头结点的过程,而写的过程就是不断取尾节点的过程。

Buffer除了读写基础数据以外,还有一个比较重要的功能就是Buffer之间的数据交换, 还记得在官方对Buffer的介绍中写到的:

当您将数据从一个缓冲区移动到另一个缓冲区时,它会重新分配片段的持有关系,而不是跨片段复制数据。这对多线程特别有用:与网络交互的子线程可以与工作线程交换数据,而无需任何复制或多余的操作。

这里说在Buffer缓冲区之间移动数据的时候,是重新分配片段也就是Segment的持有关系,而不是跨片段的复制数据,那么它说的这个比较牛逼的过程是如何实现的呢, 来看一下实现的方法:

@Override

public void write(Buffer source, long byteCount) {

// Move bytes from the head of the source buffer to the tail of this buffer

// while balancing two conflicting goals: don’t waste CPU and don’t waste

// memory.

//

//

// Don’t waste CPU (ie. don’t copy data around).

//

// Copying large amounts of data is expensive. Instead, we prefer to

// reassign entire segments from one buffer to the other.

//

//

// Don’t waste memory.

//

// As an invariant, adjacent pairs of segments in a buffer should be at

// least 50% full, except for the head segment and the tail segment.

//

// The head segment cannot maintain the invariant because the application is

// consuming bytes from this segment, decreasing its level.

//

// The tail segment cannot maintain the invariant because the application is

// producing bytes, which may require new nearly-empty tail segments to be

// appended.

//

//

// Moving segments between buffers

//

// When writing one buffer to another, we prefer to reassign entire segments

// over copying bytes into their most compact form. Suppose we have a buffer

// with these segment levels [91%, 61%]. If we append a buffer with a

// single [72%] segment, that yields [91%, 61%, 72%]. No bytes are copied.

//

// Or suppose we have a buffer with these segment levels: [100%, 2%], and we

// want to append it to a buffer with these segment levels [99%, 3%]. This

// operation will yield the following segments: [100%, 2%, 99%, 3%]. That

// is, we do not spend time copying bytes around to achieve more efficient

// memory use like [100%, 100%, 4%].

//

// When combining buffers, we will compact adjacent buffers when their

// combined level doesn’t exceed 100%. For example, when we start with

// [100%, 40%] and append [30%, 80%], the result is [100%, 70%, 80%].

//

//

// Splitting segments

//

// Occasionally we write only part of a source buffer to a sink buffer. For

// example, given a sink [51%, 91%], we may want to write the first 30% of

// a source [92%, 82%] to it. To simplify, we first transform the source to

// an equivalent buffer [30%, 62%, 82%] and then move the head segment,

// yielding sink [51%, 91%, 30%] and source [62%, 82%].

if (source == null) throw new IllegalArgumentException(“source == null”);

if (source == this) throw new IllegalArgumentException(“source == this”);

checkOffsetAndCount(source.size, 0, byteCount);

while (byteCount > 0) {

// Is a prefix of the source’s head segment all that we need to move?

// 如果 Source Buffer 的头结点可用字节数大于要写出的字节数

if (byteCount < (source.head.limit - source.head.pos)) {

//取到当前buffer的尾节点

Segment tail = head != null ? head.prev : null;

// 如果尾部结点有足够空间可以写数据,并且这个结点是底层数组的拥有者

if (tail != null && tail.owner

&& (byteCount + tail.limit - (tail.shared ? 0 : tail.pos) <= Segment.SIZE)) {

// Our existing segments are sufficient. Move bytes from source’s head to our tail.

//source头结点的数据写入到当前尾节点中,然后就直接结束返回了

source.head.writeTo(tail, (int) byteCount);

source.size -= byteCount;

size += byteCount;

return;

} else {

// We’re going to need another segment. Split the source’s head

// segment in two, then move the first of those two to this buffer.

//如果尾节点空间不足或者不是持有者,这时就需要把 Source Buffer 的头结点分割为两个 Segment,

//然后将source的头指针更新为分割后的第一个Segment, 如[92%, 82%]变成[30%, 62%, 82%]这样

source.head = source.head.split((int) byteCount);

}

}

// Remove the source’s head segment and append it to our tail.

//从 Source Buffer 的链表中移除头结点, 并加入到当前Buffer的链尾

Segment segmentToMove = source.head;

long movedByteCount = segmentToMove.limit - segmentToMove.pos;

//移除操作,并移动更新source中的head

source.head = segmentToMove.pop();

// 如果当前buffer的头结点为 null,则头结点直接指向source的头结点,初始化双向链表

if (head == null) {

head = segmentToMove;

head.next = head.prev = head;

} else {

//否则就把Source Buffer的 head 加入到当前Buffer的链尾

Segment tail = head.prev;

tail = tail.push(segmentToMove);//压入链尾,并更新尾节点

tail.compact();//尾节点尝试合并,如果合并成功,则尾节点会被SegmentPool回收掉

}

source.size -= movedByteCount;

size += movedByteCount;

byteCount -= movedByteCount;

}

}

主要就是在这个write(Buffer source, long byteCount)方法中实现的,这个方法前面有大段的英文注释,我从源码中直接复制过来的,我们可以翻译过来理解一下说的是啥:

将字节数据从source buffer的头节点复制到当前buffer的尾节点中,这里主要需要平衡两个相互冲突的目标:CPU内存

不要浪费CPU(即不要复制全部的数据)

复制大量数据代价昂贵。相反,我们更喜欢将整个段从一个缓冲区重新分配到另一个缓冲区。

不要浪费内存

Segment作为一个不可变量,缓冲区中除了头节点和尾节点的片段以外,相邻的片段,至少应该保证50%以上的数据负载量(指的是Segment中的data数据, Okio认为data数据量在50%以上才算是被有效利用的)。由于头结点中需要读取消耗字节数据,而尾节点中需要写入产生字节数据,因此头结点和尾节点是不能保持不变性的。

在缓冲区之间移动片段

在将一个缓冲区写入另一个缓冲区时,我们更喜欢重新分配整个段,将字节复制到最紧凑的形式。假设我们有一个缓冲区,其中的片段负载为[91%,61%],如果我们要在这上面附加一个负载量为[72%]的单一片段,这样将产生的结果为[91%,61%,72%]。这期间不会进行任何的字节复制操作。(即空间换时间,牺牲内存,提供速度)

再假设,我们有一个缓冲区负载量为:[100%,2%],并且我们希望将其附加到一个负载量为[99%,3%]的缓冲区中。这个操作将产生以下部分:[100%、2%、99%、3%],也就是说,我们不会花时间去复制字节来提高内存的使用效率,如变成[100%,100%,4%]这样。(即这种情况下Okio不会采取时间换空间的策略,因为太浪费CPU

在合并缓冲区时,当相邻缓冲区的合并级别不超过100%时,我们将压缩相邻缓冲区。例如,当我们在[100%,40%]基础上附加[30%,80%]时,结果将会是[100%,70%,80%]。(也就是中间相邻的负载为40%和30%的两个Segment将会被合并为一个负载为70%的Segment)

分割片段

有时我们只想将source buffer中的一部分写入到sink buffer当中,例如,给定一个sink为 [51%,91%],现在我们想要将一个source[92%,82%]的前30%写入到这个sink buffer当中。为了简化,我们首先将source buffer转换为等效缓冲区[30%,62%,82%](即拆分Segment),然后移动source的头结点Segment即可,最终生成sink[51%,91%,30%]和source[62%,82%]

这里的注释基本上已经说明了这个方法的意图实现过程,主要是通过移动source头结点的指向,另外配合分割/合并Segment的操作来平衡CPU消耗和内存消耗的两个目标。

Segment的合并过程

假设初始两个Buffer中的Segment链表如下:

在这里插入图片描述

现在将第二个Buffer完全写入到第一个Buffer

在这里插入图片描述

首先,它会直接将第二个Buffer的头节点连接到第一个Buffer的链尾,然后尝试将链尾的两个Segment进行合并,如果合并成功,则在合并之后,图中40%的那个Segment会被SegmentPool回收,它的数据完全写入到30%的那个Segment中,最终生成一个70%Segment,这样就达到了节约内存的目标。

Segment的拆分过程

假设初始两个Buffer中的Segment链表如下:

在这里插入图片描述

现在要从第二个Buffer中取前30%的数据写入到第一个Buffer当中,那么首先会将第二个Buffer的头结点Segment进行分割,分割为两个负载为30%62%Segment, 接下来移动这个新的30%Segment节点到第一个Buffer的链表的尾部:

在这里插入图片描述

这样就完成了从第二个Buffer30%的数据写入到第一个Buffer当中的工作。

ByteString

ByteString是一个不可变的字节序列,它的内部实现比较简单,有两个主要的数据成员对象:

final byte[] data;

transient String utf8; // Lazily computed.

分别存储字节数据和utf-8形式的字符串数据,它有很多方法类似于java的String 如substring()startsWith()endsWith()indexOf()等,它拥有一个传递字节数组的构造函数:

ByteString(byte[] data) {

this.data = data; // Trusted internal constructor doesn’t clone data.

}

配合Buffer中的readByteString()方法,可以将任何一个对象转换成ByteString。

从源码来看这个类的主要作用是进行一些编码和哈希转换,里面有大量的转换方法:

/** Constructs a new {@code String} by decoding the bytes as {@code UTF-8}. */

public String utf8() {

String result = utf8;

// We don’t care if we double-allocate in racy code.

return result != null ? result : (utf8 = new String(data, Util.UTF_8));

}

public String base64() {

return Base64.encode(data);

}

/** Returns the 128-bit MD5 hash of this byte string. */

public ByteString md5() {

return digest(“MD5”);

}

/** Returns the 160-bit SHA-1 hash of this byte string. */

public ByteString sha1() {

return digest(“SHA-1”);

}

/** Returns the 256-bit SHA-256 hash of this byte string. */

public ByteString sha256() {

return digest(“SHA-256”);

}

/** Returns the 512-bit SHA-512 hash of this byte string. */

public ByteString sha512() {

return digest(“SHA-512”);

}

/** Returns this byte string encoded in hexadecimal. */

public String hex() {

char[] result = new char[data.length * 2];

int c = 0;

for (byte b : data) {

result[c++] = HEX_DIGITS[(b >> 4) & 0xf];

result[c++] = HEX_DIGITS[b & 0xf];

}

return new String(result);

}

另外还有一些静态方法,也是用来编码转换的:

在这里插入图片描述

这样看来,基本上,你可以把ByteString当成一个工具类来用了。

Timeout

Timeout是Okio中的超时机制,Okio对source和sink都提供了超时机制的访问,我们在调用Okio.source()或者Okio.sink()的时候会默认携带一个Timeout的对象:

fun InputStream.source(): Source = InputStreamSource(this, Timeout())

在InputStreamSource的read方法中会调用timeout.throwIfReached()进行超时判断:

override fun read(sink: Buffer, byteCount: Long): Long {

if (byteCount == 0L) return 0

require(byteCount >= 0) { “byteCount < 0: $byteCount” }

try {

timeout.throwIfReached()

val tail = sink.writableSegment(1)

val maxToCopy = minOf(byteCount, Segment.SIZE - tail.limit).toInt()

val bytesRead = input.read(tail.data, tail.limit, maxToCopy)

if (bytesRead == -1) return -1

tail.limit += bytesRead

sink.size += bytesRead

return bytesRead.toLong()

} catch (e: AssertionError) {

if (e.isAndroidGetsocknameError) throw IOException(e)

throw e

}

}

timeout.throwIfReached()方法的实现:

public void throwIfReached() throws IOException {

if (Thread.interrupted()) {

throw new InterruptedIOException(“thread interrupted”);

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

结尾

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。

这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家~

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img
存中…(img-fSai90iK-1712757325431)]
[外链图片转存中…(img-QSJtyxme-1712757325431)]
[外链图片转存中…(img-lEltzmRy-1712757325432)]
[外链图片转存中…(img-hTaYcSBl-1712757325432)]
[外链图片转存中…(img-WTlVoy0l-1712757325432)]
[外链图片转存中…(img-QTS731Lj-1712757325433)]
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-NRX0ZSVK-1712757325433)]

结尾

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。

这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家~

[外链图片转存中…(img-QbNVcdRc-1712757325433)]

[外链图片转存中…(img-FUbg9PEQ-1712757325433)]

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-ApNuHwxx-1712757325434)]

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Android天气预报应用程序示例,使用了OpenWeatherMap API获取天气数据: 1. 创建一个新的Android项目,并在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 2. 在build.gradle文件中添加以下依赖项: ```groovy implementation 'com.squareup.okhttp3:okhttp:4.9.1' implementation 'com.squareup.okio:okio:2.9.0' implementation 'com.google.code.gson:gson:2.8.6' ``` 3. 在activity_main.xml文件中添加以下视图: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" android:orientation="vertical"> <EditText android:id="@+id/edit_text_city" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter city name" /> <Button android:id="@+id/btn_get_weather" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Get Weather" /> <TextView android:id="@+id/text_view_weather" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> </LinearLayout> ``` 4. 在MainActivity.java文件中添加以下代码: ```java import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.google.gson.Gson; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; import java.io.IOException; public class MainActivity extends AppCompatActivity { private EditText editTextCity; private Button btnGetWeather; private TextView textViewWeather; private OkHttpClient client; private Gson gson; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextCity = findViewById(R.id.edit_text_city); btnGetWeather = findViewById(R.id.btn_get_weather); textViewWeather = findViewById(R.id.text_view_weather); client = new OkHttpClient(); gson = new Gson(); handler = new Handler(Looper.getMainLooper()); btnGetWeather.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String city = editTextCity.getText().toString(); if (city.isEmpty()) { return; } getWeather(city); } }); } private void getWeather(final String city) { Request request = new Request.Builder() .url("https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid={your_api_key}") .build(); client.newCall(request).enqueue(new com.squareup.okhttp.Callback() { @Override public void onFailure(Request request, IOException e) { handler.post(new Runnable() { @Override public void run() { textViewWeather.setText("Failed to fetch weather data"); } }); } @Override public void onResponse(Response response) throws IOException { if (!response.isSuccessful()) { onFailure(null, null); return; } final String json = response.body().string(); final WeatherData weatherData = gson.fromJson(json, WeatherData.class); handler.post(new Runnable() { @Override public void run() { textViewWeather.setText(String.format("Temperature in %s: %.1f°C", city, weatherData.main.temp - 273.15)); } }); } }); } private static class WeatherData { MainData main; private static class MainData { double temp; } } } ``` 其中,{your_api_key}应替换为您的OpenWeatherMap API密钥。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值