0

When I create an instance of OkHttpClient in my MainActivity the app crash and shows a fatal error. I create the instance of OkHttpClient but at the start of the method the app crashes. This is my class

public class OkHttpHandler extends AsyncTask {
        OkHttpClient client = new OkHttpClient();

        @Override
        protected Object doInBackground(Object[] objects) {
            MediaType mediaType = MediaType.parse("application/json");
            Request.Builder builder = new Request.Builder();
            builder.url("https://api.tinyurl.com/create");
            RequestBody body = RequestBody.create(mediaType, "{\n  \"url\":             \"https://www.google.com/maps/place/20.6738673,-103.3766127\",\n  \"domain\": \"tinyurl.com\",\n  \"alias\": \"\",\n  \"tags\": \"\",\n  \"expires_at\": \"\"\n}");
            Request request = new Request.Builder()
                    .url("https://api.tinyurl.com/create")
                    .method("POST", body)
                    .addHeader("accept", "application/json")
                    .addHeader("Authorization", "Bearer @token")
                    .addHeader("Content-Type", "application/json")
                    .build();
            try {
                Response response = client.newCall(request).execute();
                return response.body().string();
            }catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }
    }

AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.alertaminerva, PID: 11113
    java.lang.ExceptionInInitializerError
        at okhttp3.OkHttpClient.<init>(OkHttpClient.kt:219)
        at okhttp3.OkHttpClient.<init>(OkHttpClient.kt:211)
        at com.example.alertaminerva.MainActivity.<init>(MainActivity.java:54)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)

0 Answers0