javaeclipseokhttpokio

Okhttp3 jar missing okio?


I am trying to make a simple Java program to upload a bunch of images I have to imgur. But I am running into problem after problem and cannot just get okhttp to work. At this point the time I have spent trying to solve this has been way longer than it will take for me to write the program itself. I am very new to this kind of stuff so be patient with me please.

So, right now I have the following code from this tutorial:

RequestBody requestBody = new MultipartBody.Builder()
                .addFormDataPart("new", "This is my new TODO")
                .addFormDataPart("image", "attachment.png",
                        RequestBody.create(new File(""), MediaType.parse("image/png"))
                )
                .setType(MultipartBody.FORM)
                .build();

Which is giving an error on the RequestBody.create() part:

The type okio.ByteString cannot be resolved. It is indirectly referenced from required .class files

When Googling this error, I find this page which says I'm missing the okio library. I thought this would be included with the okhttp jar. I download the okio jar anyway and add it to my project, but the error doesn't change. I have no idea what else might be wrong.


Solution

  • Ok, I figured out your problem.

    Okio source code from 3.0.0-Alpha-10 and above has been re-written with Kotlin.

    Use this version https://repo1.maven.org/maven2/com/squareup/okio/okio/3.0.0-alpha.9/ This one is written in Java, before the migration to Kotlin.

    The code below will compile:

    package example;
    
    import java.io.File;
    import okhttp3.MediaType;
    import okhttp3.MultipartBody;
    import okhttp3.RequestBody;
    
    
    public class OkHttpExample {
    
        public void example() {
            
            RequestBody requestBody = new MultipartBody.Builder()
                    .addFormDataPart("new", "This is my new TODO")
                    .addFormDataPart("image", "attachment.png",
                            RequestBody.create(new File(""), MediaType.parse("image/png"))
                    )
                    .setType(MultipartBody.FORM)
                    .build();
        }
        
    }
    

    See build path dependencies:

    enter image description here

    Once in your build path, in eclipse you can open the jar file, and see the contents: ByteString.class is included:

    enter image description here