okhttp 对应java版本_java – Android |在运行时获取OkHTTP库版本

本文探讨了OkHttp在Google API中的应用以及与HttpURLConnection的关系。分析了HttpURLConnectionImpl类的具体实现,包括如何发送请求和接收响应,以及如何通过反射获取默认用户代理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从4.xx谷歌正在使用okhttp部分的平方

/**

* This implementation uses HttpEngine to send requests and receive responses. This class may use

* multiple HttpEngines to follow redirects, authentication retries, etc. to retrieve the final

* response body.

*

*

What does 'connected' mean?

This class inherits a {@code connected} field from the

* superclass. That field is not used to indicate whether this URLConnection is

* currently connected. Instead, it indicates whether a connection has ever been attempted. Once a

* connection has been attempted, certain properties (request header fields, request method, etc.)

* are immutable.

*/

public class HttpURLConnectionImpl extends HttpURLConnection {

private String defaultUserAgent() {

String agent = System.getProperty("http.agent");

return agent != null ? Util.toHumanReadableAscii(agent) : Version.userAgent();

}

一切都取决于设备 – 你使用的os版本,因为api正在发展,你可以使用反射,但你需要知道特定的api是什么字段

你可以在一开始就试试

System.getProperty("http.agent");

编辑:

通过反思

HttpURLConnection connection = (HttpURLConnection) new URL("http://google.com")

.openConnection();

Method method = connection.getClass().getDeclaredMethod("defaultUserAgent");

method.setAccessible(true);

String okEngineVersion = (String) method.invoke(connection, new Object[]{});

与…一样

String okEngineVersion = System.getProperty("http.agent");

如果你想打扰:

>每个班级都以同样的方式对待 – > as equals(没有版本控制 – 你只能查看魔术次要主编号 – 来自类的java编译器版本)

> /system/framework/okhttp.jar的清单不包含版本属性

如果你想要okhttp.internal.Version类,那么:

File file = new File("/system/framework/okhttp.jar");

// using javaxt-core lib

Jar jar = new Jar(file);

jar.getVersion();

// load dex

DexFile dexfile = DexFile.loadDex(file.getAbsolutePath(),

File.createTempFile("opt", "dex", _context.getCacheDir()).getPath(), 0);

Enumeration dexEntries = dexfile.entries();

ClassLoader systemClassLoader = DexClassLoader.getSystemClassLoader();

while (dexEntries.hasMoreElements()) {

String className = dexEntries.nextElement();

Class> aClass = systemClassLoader.loadClass(className);

}

conclusion: If you want to avoid crash of app from library changes delivery own version of library and load classes on the fly or compile with apk

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值