如何调试 “com.android.okhttp”

2022-09-03 16:07:56

在 android kitkat 中,URLConnection 的实现已经被 OkHttp 所取代,它该如何调试呢?

OkHttp 位于此目录中:external/okhttp/android/main/java/com/squareup/okhttp

当我调用 时,它存在UrlInstance.openConnection().getClass().getName()com.android.okhttp.internal.http.HttpURLConnectionImpl

我该如何调试它?看来我不能将/android/main/java/com/squareup/okhttp/*com.android.okhttp.*

当代码执行到return streamHandler.openConnection(this);

/**
 * Returns a new connection to the resource referred to by this URL.
 *
 * @throws IOException if an error occurs while opening the connection.
 */
public URLConnection openConnection() throws IOException {
    return streamHandler.openConnection(this);
}

往前走,但挖不透com.squareup.okhttp.HttpHandler#openConnection

下图中调试器中突出显示的线程为灰色。

package com.squareup.okhttp;

import java.io.IOException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

public class HttpHandler extends URLStreamHandler {
    @Override protected URLConnection openConnection(URL url) throws IOException {
        return newOkHttpClient(null /* proxy */).open(url);
    }

    @Override protected URLConnection openConnection(URL url, Proxy proxy) throws IOException {
        if (url == null || proxy == null) {
            throw new IllegalArgumentException("url == null || proxy == null");
        }
        return newOkHttpClient(proxy).open(url);
    }

    @Override protected int getDefaultPort() {
        return 80;
    }

    protected OkHttpClient newOkHttpClient(Proxy proxy) {
        OkHttpClient client = new OkHttpClient();
        client.setFollowProtocolRedirects(false);
        if (proxy != null) {
            client.setProxy(proxy);
        }

        return client;
    }
}

enter image description here


答案 1

从 Android Studio 的 0.8.x 版本开始,似乎已删除在 Android SDK 上附加特定源的选项(https://plus.google.com/+CyrilMottier/posts/GNcGL6xVth1 在此处阅读评论)。

我想如果你想调试,你可以尝试旧版本或Android Studio,或者(我会使用这个解决方案)将应用程序的一部分导出到Eclipse(Android SDK版本),也许只是涉及麻烦连接的一部分,在那里你可以非常轻松地附加一个在调试期间没有附加源代码的类的源代码, 您应该在源代码视图中看到一个仅包含方法签名的页面,其中包含“附加源代码”按钮

http://4.bp.blogspot.com/-a2EnbC4wP6g/UN2Z7QzFlyI/AAAAAAAAAg0/D2tFh6AgRrM/s1600/Eclipse_String_Class_File.PNG


答案 2

推荐