Ok.sttup请求在com.squareup.okhttp中不公开;无法从外部包裹访问

时间:2015-08-07 20:09:08

标签: compiler-errors okhttp

我试图让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')

}

但错误&#34;错误:(8,27)错误:请求在com.squareup.okhttp中不公开;无法从外部包裹访问&#34;和&#34;错误:(9,27)错误:com.squareup.okhttp中的响应不公开;无法从外部包裹访问&#34;发生在ParseResponseWithGson类中的代码:

import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

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

2 个答案:

答案 0 :(得分:0)

你的类路径上可能还有另一个过时的OkHttp版本。旧版本的请求不公开,但2.0+版本的请求是。

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

答案 1 :(得分:0)

看起来行compile files('libs/GlassVoice-xe22.jar')导致了这个问题,我想它包含一个过时的OkHttp版本,因为一旦删除该行,代码就可以正常工作。