0

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.

parakeet
  • 53
  • 6

1 Answers1

2

Ok, I figured out your problem.

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

Your code is requiring ByteString.class, for this you need a Java .class.

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

JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • I am using Eclipse, I have both libraries in the /lib folder and both of them on my classpath. You say I need to get both the classpath and IDE to point to the libraries but I am unsure what getting the IDE to point to the library means. I am using okhttp3 4.9.2 and okio 3.0.0 if this helps. – parakeet Nov 16 '21 at 22:11
  • @parakeet Are you running from inside Eclipse? Or from the Command Line? – JCompetence Nov 17 '21 at 21:09
  • From inside Eclipse – parakeet Nov 18 '21 at 03:24
  • see edit @parakeet Okio from version 3.0.0-Alpha-10 has been refactored to use kotlin instead of Java. You will need to use a version earlier, Alpha-9 to be able to work with it. – JCompetence Nov 18 '21 at 20:35
  • THANK YOU! It didn't work at first and I was getting kotlin errors so I tried downgrading okhttp to version 3.14.9 before realizing it was a problem with okio still and not okhttp. Okio version 1.9.0 works for me. I wouldn't have figured this out myself, I really appreciate the help! – parakeet Nov 19 '21 at 08:10
  • @parakeet You learned something new, so that is great :) – JCompetence Nov 19 '21 at 08:16
  • Okio 2 is kotlin already. https://medium.com/square-corner-blog/okio-2-6f6c35149525 – Yuri Schimke Nov 21 '21 at 06:36
  • @YuriSchimke That is true, but not the source code. The source code was still Java Compatible. `The update is also Java source-compatible. When you configure your pom.xml or build.gradle to use the new version you won’t have to change any .java code to get a clean build. (Our changelog notes one minor exception to this, unrelated to the Kotlin transition.) But the update is not Kotlin source-compatible; ` – JCompetence Nov 21 '21 at 06:56
  • You only have binaries of okhttp and okio, and your code in a java file. So I don't understand your answer. "Your code is requiring ByteString.class, for this you need a Java .class." – Yuri Schimke Nov 21 '21 at 06:59