Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug "com.android.okhttp"

In android kitkat, URLConnection's implementation has been replaced by OkHttp,How can it debug it?

The OkHttp is in this directory:external/okhttp/android/main/java/com/squareup/okhttp

When i call the UrlInstance.openConnection().getClass().getName(), it present com.android.okhttp.internal.http.HttpURLConnectionImpl

How can i debug the it ? It seems that i can't associate the /android/main/java/com/squareup/okhttp/* to the com.android.okhttp.*

When the code excute to the 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);
}

Go forward further,but can't dig into the com.squareup.okhttp.HttpHandler#openConnection

The The highlighted thread in debugger in the picture below is gray.

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

like image 230
log1000 Avatar asked Apr 19 '14 09:04

log1000


People also ask

Does Android use OkHttp?

squareup. okhttp , artifactId, okhttp and the version 2.5. 0 (current as of this writing). As of Android 5.0, OkHttp is part of the Android platform and is used for all HTTP calls.

What is the use of OkHttp?

OkHttp is an HTTP client from Square for Java and Android applications. It's designed to load resources faster and save bandwidth. OkHttp is widely used in open-source projects and is the backbone of libraries like Retrofit, Picasso, and many others.

What is the underlying library of OkHttp?

Beginning with Mobile SDK 4.2, the Android REST request system uses OkHttp (v3. 2.0), an open-source external library from Square Open Source, as its underlying architecture. This library replaces the Google Volley library from past releases.

Is OkHttp fast?

Results show that OkHttp achieves slightly better performance when compared with HttpURLConnection for transfers of larger files, by transferring the same file almost 100ms faster on a smartphone and 500ms faster in the emulator.


1 Answers

It seems from version 0.8.x onwards of Android Studio the option to attach specific sources at the Android SDK has been removed (Read comments here https://plus.google.com/+CyrilMottier/posts/GNcGL6xVth1).

I guess if you want to debug you could try an older version or Android Studio, or (and I'd go with this solution) export a portion of your application to Eclipse (the Android SDK version), maybe just the part involving the troublesome connection, and there you can attach sources for a class that has no sources attached during debug very easily, you should see a page with just the signatures of the methods in the Source Code View, with the button to "Attach source"

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

like image 80
alessiop86 Avatar answered Oct 06 '22 01:10

alessiop86