question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Why PackageName is "com.android.okhttp" not "com.squareup.okhttp"?

See original GitHub issue

Hello @swankjesse

Q1

I have a problem that how android 5.1 integration with Okhttp?

I can not find com.android.okhttp.internal.http.HttpURLConnectionImpl in android aosp5.11 (http://androidxref.com/),

but find com.squareup.okhttp.internal.huc.HttpURLConnectionImpl


Q2

Can i pass my custom URLStreamHandler impl to

java.net.URL#URL(java.net.URL, java.lang.String,  java.net.URLStreamHandler)

in order to insteading of the internal-okhttp-moudle in android aosp 5.1? Because i want avoid some bug in special brand devices.

my coustom URLStreamHandler impl :

/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.squareup.okhttp;

import libcore.net.NetworkSecurityPolicy;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.ResponseCache;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class HttpHandler extends URLStreamHandler {

    private final static List<ConnectionSpec> CLEARTEXT_ONLY =
        Collections.singletonList(ConnectionSpec.CLEARTEXT);

    private final ConfigAwareConnectionPool configAwareConnectionPool =
            ConfigAwareConnectionPool.getInstance();

    @Override protected URLConnection openConnection(URL url) throws IOException {
        return newOkUrlFactory(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 newOkUrlFactory(proxy).open(url);
    }

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

    protected OkUrlFactory newOkUrlFactory(Proxy proxy) {
        OkUrlFactory okUrlFactory = createHttpOkUrlFactory(proxy);
        // For HttpURLConnections created through java.net.URL Android uses a connection pool that
        // is aware when the default network changes so that pooled connections are not re-used when
        // the default network changes.
        okUrlFactory.client().setConnectionPool(configAwareConnectionPool.get());
        return okUrlFactory;
    }

    /**
     * Creates an OkHttpClient suitable for creating {@link java.net.HttpURLConnection} instances on
     * Android.
     */
    // Visible for android.net.Network.
    public static OkUrlFactory createHttpOkUrlFactory(Proxy proxy) {
        OkHttpClient client = new OkHttpClient();

        // Explicitly set the timeouts to infinity.
        client.setConnectTimeout(0, TimeUnit.MILLISECONDS);
        client.setReadTimeout(0, TimeUnit.MILLISECONDS);
        client.setWriteTimeout(0, TimeUnit.MILLISECONDS);

        // Set the default (same protocol) redirect behavior. The default can be overridden for
        // each instance using HttpURLConnection.setInstanceFollowRedirects().
        client.setFollowRedirects(HttpURLConnection.getFollowRedirects());

        // Do not permit http -> https and https -> http redirects.
        client.setFollowSslRedirects(false);

        if (NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted()) {
          // Permit cleartext traffic only (this is a handler for HTTP, not for HTTPS).
          client.setConnectionSpecs(CLEARTEXT_ONLY);
        } else {
          // Cleartext HTTP denied by policy. Make okhttp deny cleartext HTTP attempts using the
          // only mechanism it currently provides -- pretend there are no suitable routes.
          client.setConnectionSpecs(Collections.<ConnectionSpec>emptyList());
        }

        // When we do not set the Proxy explicitly OkHttp picks up a ProxySelector using
        // ProxySelector.getDefault().
        if (proxy != null) {
            client.setProxy(proxy);
        }

        // OkHttp requires that we explicitly set the response cache.
        OkUrlFactory okUrlFactory = new OkUrlFactory(client);
        ResponseCache responseCache = ResponseCache.getDefault();
        if (responseCache != null) {
            AndroidInternal.setResponseCache(okUrlFactory, responseCache);
        }
        return okUrlFactory;
    }

}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
JakeWhartoncommented, Jan 6, 2016

I can not find *com.android.okhttp.internal.http.HttpURLConnectionImpl * in android aosp5.11 (http://androidxref.com/),

but find *com.squareup.okhttp.internal.huc.HttpURLConnectionImpl *

OkHttp in Android is here: https://android.googlesource.com/platform/external/okhttp/+/master. The reason the package name is com.android.okhttp is because there are jarjar rules which repackage it under that name.

0reactions
beaumontkcommented, Feb 6, 2017

I cloned: https://android.googlesource.com/platform/external/okhttp/ How do I build okhttp.jar for Android? mvn, ndk-build, other? mvn gives errors. ndk-build looks for jni dir and needs Application.mk Please explain. KB

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android: can not resolve com.squareup.okhttp - Stack Overflow
i added compile 'com.squareup.okhttp:okhttp-urlconnection:2.5.0' to gradle but still it can't convert okhttp3.OkHttpClient to com.squareup.
Read more >
Using OkHttp | CodePath Android Cliffnotes
OkHttp is a third-party library developed by Square for sending and receive HTTP-based network requests. It is built on top of the Okio...
Read more >
Change Log - OkHttp
The new APIs use mockwebserver3 in both the Maven coordinate and package name. This new API is not stable and will likely change...
Read more >
[PROBLEM FOUND] Errors after change Http for OkHttp - B4X
Once I changed HttpUtils2 with Http to OkHttp the application began to show some errors that were not displayed. Errors like: (Line: 145)...
Read more >
okhttp not found, migrating to Android developer studio (gradle)
That should take care of downloading the Dropbox library and its dependencies for you. That way you don't have to include all of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found