从 4.xx 开始,google 正在使用 squareup 的 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.
*
* <h3>What does 'connected' mean?</h3> This class inherits a {@code connected} field from the
* superclass. That field is <strong>not</strong> 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();
}
https://github.com/square/okhttp/blob/master/okhttp-urlconnection/src/main/java/okhttp3/internal/huc/HttpURLConnectionImpl.java
http://square.github.io/okhttp/
一切都取决于设备 - 你使用什么操作系统版本,因为 api 正在发展,你可以使用反射,但你需要知道特定 api 上的字段是什么
见https://github.com/square/okhttp/blob/master/CHANGELOG.md
比较不同的 api 版本使用:https://android.googlesource.com/platform/external/okhttp/
你可以先试试
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");
如果你想打扰:
- 每个类都以相同的方式处理 -> 平等(没有版本控制 - 你只能检查魔术次要主编号 - 从类中获取 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<String> dexEntries = dexfile.entries();
ClassLoader systemClassLoader = DexClassLoader.getSystemClassLoader();
while (dexEntries.hasMoreElements()) {
String className = dexEntries.nextElement();
Class<?> aClass = systemClassLoader.loadClass(className);
}
结论:如果您想避免应用程序崩溃从库更改交付
自己的库版本和动态加载类或使用 apk 编译