OkHttp Request 不是 public in com.squareup.okhttp;无法从外部包访问
OkHttp Request is not public in com.squareup.okhttp; cannot be accessed from outside package
我试图让 OkHttp 在我的 Android Studio 项目中工作,我创建了一个 class 并从这里粘贴了以下代码: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')
}
但错误 "Error:(8, 27) error: Request is not public in com.squareup.okhttp; cannot be accessed from outside package" 和 "Error:(9, 27) error: Response is not public in com.squareup.okhttp; cannot be accessed from outside package" 出现在代码的 ParseResponseWithGson class 中:
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
有谁知道我该如何解决这些错误?似乎构造函数不是 public 但这听起来很奇怪但是我已经复制并粘贴了代码所以我不确定我可能做错了什么,除非 OkHttp 和 OkIo 的错误版本不知何故参考?
您的类路径中可能有另一个过时的 OkHttp 版本。旧版本的 Request 不是 public,但 2.0+ 中的是
尝试对 libs 目录中的每个 jar 使用 jar tvf libs/foo.jar
来找到它!
看起来是 compile files('libs/GlassVoice-xe22.jar')
行引起了这个问题,我猜它包含 OkHttp 的过时版本,因为一旦我删除了该行,代码就可以正常工作。
我试图让 OkHttp 在我的 Android Studio 项目中工作,我创建了一个 class 并从这里粘贴了以下代码: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')
}
但错误 "Error:(8, 27) error: Request is not public in com.squareup.okhttp; cannot be accessed from outside package" 和 "Error:(9, 27) error: Response is not public in com.squareup.okhttp; cannot be accessed from outside package" 出现在代码的 ParseResponseWithGson class 中:
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
有谁知道我该如何解决这些错误?似乎构造函数不是 public 但这听起来很奇怪但是我已经复制并粘贴了代码所以我不确定我可能做错了什么,除非 OkHttp 和 OkIo 的错误版本不知何故参考?
您的类路径中可能有另一个过时的 OkHttp 版本。旧版本的 Request 不是 public,但 2.0+ 中的是
尝试对 libs 目录中的每个 jar 使用 jar tvf libs/foo.jar
来找到它!
看起来是 compile files('libs/GlassVoice-xe22.jar')
行引起了这个问题,我猜它包含 OkHttp 的过时版本,因为一旦我删除了该行,代码就可以正常工作。