如何调试“com.android.okhttp”

时间:2014-04-19 09:44:44

标签: java android dalvik

在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 个答案:

答案 0 :(得分:3)

从Android版的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

相关问题