在OkHttp中拒绝先前失败的类的重新初始化


22

我正在尝试使用flask后端将图像上传到我的服务器,该烧瓶将处理文件以保存在文件夹中,并在Android中保存OkHttp。但我在android中收到此错误:

I/art: Rejecting re-init on previously-failed class java.lang.Class<okhttp3.internal.platform.ConscryptPlatform$configureTrustManager$1>: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/conscrypt/ConscryptHostnameVerifier;
        at okhttp3.internal.platform.android.SocketAdapter okhttp3.internal.platform.android.ConscryptSocketAdapter.buildIfSupported() (ConscryptSocketAdapter.kt:64)
        at void okhttp3.internal.platform.AndroidPlatform.<init>() (AndroidPlatform.kt:45)
        at okhttp3.internal.platform.Platform okhttp3.internal.platform.AndroidPlatform$Companion.buildIfSupported() (AndroidPlatform.kt:239)
        at okhttp3.internal.platform.Platform okhttp3.internal.platform.Platform$Companion.findPlatform() (Platform.kt:211)
        at okhttp3.internal.platform.Platform okhttp3.internal.platform.Platform$Companion.access$findPlatform(okhttp3.internal.platform.Platform$Companion) (Platform.kt:179)
        at void okhttp3.internal.platform.Platform.<clinit>() (Platform.kt:180)
        at void okhttp3.OkHttpClient.<init>(okhttp3.OkHttpClient$Builder) (OkHttpClient.kt:219)
        at void okhttp3.OkHttpClient.<init>() (OkHttpClient.kt:211)
        at void com.example.swiftpinx.Activity.AddMediaActivity.onClick(android.view.View) (AddMediaActivity.java:207)
        at boolean android.view.View.performClick() (View.java:5637)
        at void android.view.View$PerformClick.run() (View.java:22429)
        at void android.os.Handler.handleCallback(android.os.Message) (Handler.java:751)
        at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:95)
        at void android.os.Looper.loop() (Looper.java:154)
        at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6119)
        at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
        at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:886)
        at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:776)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "org.conscrypt.ConscryptHostnameVerifier" on path: DexPathList[[zip file "/data/app/com.example.swiftpinx-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.swiftpinx-1/lib/x86, /system/lib, /vendor/lib]]
        at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
        at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
        at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
        at okhttp3.internal.platform.android.SocketAdapter okhttp3.internal.platform.android.ConscryptSocketAdapter.buildIfSupported() (ConscryptSocketAdapter.kt:64)
        at void okhttp3.internal.platform.AndroidPlatform.<init>() (AndroidPlatform.kt:45)
        at okhttp3.internal.platform.Platform okhttp3.internal.platform.AndroidPlatform$Companion.buildIfSupported() (AndroidPlatform.kt:239)
        at okhttp3.internal.platform.Platform okhttp3.internal.platform.Platform$Companion.findPlatform() (Platform.kt:211)
        at okhttp3.internal.platform.Platform okhttp3.internal.platform.Platform$Companion.access$findPlatform(okhttp3.internal.platform.Platform$Companion) (Platform.kt:179)
        at void okhttp3.internal.platform.Platform.<clinit>() (Platform.kt:180)
        at void okhttp3.OkHttpClient.<init>(okhttp3.OkHttpClient$Builder) (OkHttpClient.kt:219)
        at void okhttp3.OkHttpClient.<init>() (OkHttpClient.kt:211)
        at void com.example.swiftpinx.Activity.AddMediaActivity.onClick(android.view.View) (AddMediaActivity.java:207)
        at boolean android.view.View.performClick() (View.java:5637)
        at void android.view.View$PerformClick.run() (View.java:22429)
        at void android.os.Handler.handleCallback(android.os.Message) (Handler.java:751)
        at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:95)
        at void android.os.Looper.loop() (Looper.java:154)
        at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6119)
        at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
        at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:886)
        at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:776)

并尝试在网上找到解决此问题的解决方案,但没有任何办法可以解决我的情况。

也许您可能需要我的源代码,在这里:

package com.example.swiftpinx.Activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.example.swiftpinx.Adapters.AddMediaAdapter;
import com.example.swiftpinx.Link.UrlLink;
import com.example.swiftpinx.R;

import org.jetbrains.annotations.NotNull;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AddMediaActivity extends AppCompatActivity implements
        LoaderManager.LoaderCallbacks<Cursor>, AddMediaAdapter.OnClickThumbnail,
        View.OnClickListener {

    /*
        Set up's
     */

    private static final String TAG = "AddMediaActivity";

    private final static int READ_EXTERNAL_STORAGE_REQUEST_CODE = 0;
    private final static int MEDIASTORE_LOADER_ID = 0;

    AddMediaAdapter addMediaAdapter;

    Uri imagePath;

    /*
        Pallete
     */

    RecyclerView rvGallery;

    ImageView ivImage;

    LinearLayout llPostEvent;

    Button btnPost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_media);

        rvGallery = (RecyclerView) findViewById(R.id.rvGallery);
        rvGallery.setLayoutManager(new GridLayoutManager(this, 3));

        addMediaAdapter = new AddMediaAdapter(this);

        rvGallery.setAdapter(addMediaAdapter);

        ivImage = (ImageView) findViewById(R.id.ivImage);

        llPostEvent = (LinearLayout) findViewById(R.id.llPostEvent);

        btnPost = (Button) findViewById(R.id.btnPost);

        btnPost.setOnClickListener(this);

        llPostEvent.setVisibility(View.GONE);

        checkPermissionEnternalStorage();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch(requestCode) {
            case READ_EXTERNAL_STORAGE_REQUEST_CODE:
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    LoaderManager.getInstance(this).initLoader(MEDIASTORE_LOADER_ID, null, this);
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        }
    }

    private void checkPermissionEnternalStorage() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED) {
                LoaderManager.getInstance(this).initLoader(MEDIASTORE_LOADER_ID, null, this);
            } else {
                if(shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    // explain here.
                    Toast.makeText(this, "Swiftpin wants to view your gallery.", Toast.LENGTH_SHORT).show();
                }

                requestPermissions(new String[] { Manifest.permission.READ_EXTERNAL_STORAGE },
                        READ_EXTERNAL_STORAGE_REQUEST_CODE);
            }
        } else {
            // load default
            LoaderManager.getInstance(this).initLoader(MEDIASTORE_LOADER_ID, null, this);
        }
    }

    @NonNull
    @Override
    public Loader<Cursor> onCreateLoader(int id, @Nullable Bundle args) {
        String[] projection = {
                MediaStore.Files.FileColumns._ID,
                MediaStore.Files.FileColumns.DATE_ADDED,
                MediaStore.Files.FileColumns.DATA,
                MediaStore.Files.FileColumns.MEDIA_TYPE
        };

        String condition = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE + " OR "
                + MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;

        return new CursorLoader(
                this,
                MediaStore.Files.getContentUri("external"),
                projection,
                condition,
                null,
                MediaStore.Files.FileColumns.DATE_ADDED + " DESC"
        );
    }

    @Override
    public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data) {
        addMediaAdapter.changeCursor(data);
    }

    @Override
    public void onLoaderReset(@NonNull Loader<Cursor> loader) {
        addMediaAdapter.changeCursor(null);
    }

    @Override
    public void clickThumbnail(Uri uri) {
        imagePath = uri;
        llPostEvent.setVisibility(View.VISIBLE);
        Glide.with(this)
                .load("file://" + uri)
                .centerCrop()
                .into(ivImage);
    }

    @Override
    public void onClick(View view) {
        if(view == btnPost) {
            String fileImage = String.valueOf(imagePath);

            try {
                ByteArrayOutputStream stream  = new ByteArrayOutputStream();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.RGB_565;

                Bitmap bitmap = BitmapFactory.decodeFile(fileImage, options);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byte[] byteArray = stream.toByteArray();

                RequestBody requestBody = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addFormDataPart("image", "image.jpg",
                                RequestBody.create(MediaType.parse("image/*jpg"), byteArray))
                        .build();

                Request request = new Request.Builder()
                        .url(UrlLink.postfeed)
                        .post(requestBody)
                        .build();

                OkHttpClient client = new OkHttpClient();

                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        call.cancel();

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.i(TAG, "run: Error post");
                            }
                        });
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Log.i(TAG, "run: ok -> " + response.body().toString());
                                } catch(Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                });
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}

OkHttp版本?设备?这看起来像是OkHttp错误。
杰西·威尔逊

您找到任何解决方案了吗???
Jeeva

Answers:


21

在阅读信息消息时,它使我对此有所了解Conscrypt,并进行了研究,发现我需要安装conscrypt到gradle.app

implementation 'org.conscrypt:conscrypt-android:2.2.1'

而且有效。


23
如果okHttp需要conscrypt,它应该引用该库,而不是强迫我们添加此依赖项。这不应标记为已接受。更好地在Square的github存储库上提交错误报告。
kroegerama

@kroegerama好吧okhttp 不需要 conscrypt,它是一个兼容的可选依赖项。该错误消息肯定可以得到改善,但是如果未包含conscrypt,该库将使用系统的默认实现,并且如果您将其明确设置为第一个安全提供程序,则该库将与conscrypt兼容。有关更多信息,请参见okhttp的自述文件。无论如何,我同意这不应成为公认的答案,因为警告可能很容易忽略,并且不必要地在项目中添加依赖关系是一个坏主意。
Yuval


3

OkHttp与Conscrypt安全库兼容,但是使用它是可选的。堆栈跟踪很可怕,但是我认为这只是一个警告,告诉您未找到Conscrypt依赖项。在这种情况下,OkHttp应该回退到平台的默认TLS实现。

OkHttp的自述文件

OkHttp使用平台的内置TLS实现。在Java平台上,OkHttp还支持Conscrypt,它将BoringSSL与Java集成在一起。如果OkHttp是第一个安全提供程序,它将使用Conscrypt:

Security.insertProviderAt(Conscrypt.newProvider(), 1);

长话短说,如果您的网络请求正常运行,则可以安全地忽略此堆栈跟踪。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.