我试图让OkHttp在我的Android Studio项目中工作,我创建了一个类,并从此处粘贴以下代码:https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/com/squareup/okhttp/recipes/ParseResponseWithGson.java

package com.my.project;

/**
 * Created by me on 07/08/2015.
 */
import com.google.gson.Gson;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.util.Map;

public final class ParseResponseWithGson {
private final OkHttpClient client = new OkHttpClient();
private final Gson gson = new Gson();

public void run() throws Exception {
    Request request = new Request.Builder()
            .url("https://api.github.com/gists/c2a7c39532239ff261be")
            .build();
    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    Gist gist = gson.fromJson(response.body().charStream(), Gist.class);
    for (Map.Entry<String, GistFile> entry : gist.files.entrySet()) {
        System.out.println(entry.getKey());
        System.out.println(entry.getValue().content);
    }
}

static class Gist {
    Map<String, GistFile> files;
}

static class GistFile {
    String content;
}

public static void main(String... args) throws Exception {
    new ParseResponseWithGson().run();
}
}

这是我的gradle文件:
    apply plugin: 'com.android.application'

repositories {
    jcenter()
    flatDir {
        dirs 'prebuilt-libs'
    }
}

android {
    compileSdkVersion "Google Inc.:Glass Development Kit Preview:19"
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.my.project_02"
        minSdkVersion 19
        targetSdkVersion 22
        versionCode 2
        versionName "0.2"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.squareup.okio:okio:1.5.0'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile files('libs/GlassVoice-xe22.jar')

}

但是错误“错误:(8,27)错误:com.squareup.okhttp中的请求不是公共(public)的;无法从外部程序包访问”和“错误:(9,27)错误:com.squareup中的响应不是公共(public)的.okhttp;无法从外部程序包访问”,该代码在ParseResponseWithGson类中发生:
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

有谁知道如何解决这些错误?似乎构造函数不是公开的,但这听起来很奇怪,但是我已经复制并粘贴了代码,所以我不确定自己可能做错了什么,除非以某种方式引用了OkHttp和OkIo的错误版本?

最佳答案

您的类路径上可能有另一个过时的OkHttp版本。该旧版本的请求未公开,但2.0+中的请求是公开的。

尝试对libs目录中的每个jar使用jar tvf libs/foo.jar来找到它!

关于compiler-errors - OkHttp请求不在com.squareup.okhttp中公开;无法从外部包访问,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31885854/

10-12 03:32