OkHTTP 错误无法访问 ByteString

OkHTTP error cannot access ByteString

我正在使用 OkHttp 库来分割数据,一切都很好,我没有任何错误,但是当我编译程序时,它给了我错误

Error:(172, 40) error: cannot access ByteString class file for okio.ByteString not found

此处出现错误RequestBody.create(MEDIA_TYPE_JPG, new File(data.getFileParam())))

Here is the whole code of method that implements multipart request

public static String makeRequest(RequestConstructor data, String a, String b) {
    final MediaType MEDIA_TYPE_JPG = MediaType.parse("image/jpg");
    try {
        OkHttpClient client = new OkHttpClient();

        Log.d("test",data.getFileParam());

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("bunting", data.getParam("bunting"))
                .addFormDataPart("dangler", data.getParam("dangler"))
                .addFormDataPart("poster", data.getParam("poster"))
                .addFormDataPart("tearPad", data.getParam("tearPad"))
                .addFormDataPart("leafLet", data.getParam("leafLet"))
                .addFormDataPart("outlet_category", "")
                .addFormDataPart("dealerName", data.getParam("dealerName"))
                .addFormDataPart("brouchers", data.getParam("brouchers"))
                .addFormDataPart("wobblers", data.getParam("wobblers"))
                .addFormDataPart("tentCard", data.getParam("tentCard"))
                .addFormDataPart("others", data.getParam("others"))
                .addFormDataPart("photo", "1.jpg",
                        RequestBody.create(MEDIA_TYPE_JPG, new File(data.getFileParam())))
                .build();

        Request request = new Request.Builder()
                .url(data.getUrl())
                .post(requestBody)
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        Log.d("From OkHTTP Response", response.toString());
        System.out.println(response.body().string());
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

我没有 okio 的任何导入 class 所以我尝试手动添加乐趣 android studio show 语法错误 cannot resolve symbol okio

这是一个单独的包,我们需要自己包含。名字是:奥基奥。

您可以在此处找到最新的 jar 或 java 文件:https://github.com/square/okio

您好,您可以将库 okio 添加到您的项目中。

下载Latest version

如果把两个库都加进去就可以解决。您可以查看 okhttp3 依赖项以检查您需要哪些库,在本例中为 okio。

    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.9.3</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okio</groupId>
        <artifactId>okio</artifactId>
        <version>2.8.0</version>
        <scope>compile</scope>
    </dependency>