2

I am using Netbeans and I've created java project with Maven. I added this dependency.

 <dependency>
    <groupId>com.squareup.okhttp</groupId>
    <artifactId>okhttp</artifactId>
    <version>2.7.2</version>
</dependency>

It worked, I could import com.squareup.okhttp.*. After seeing some code on the web I realized that many people are using the version 3+. I tried to change the package to:

updated I've typed groupid wrongly in the question "com.squareup.okhttp" but in my code it was right "com.squareup.okhttp3".

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.0</version>

But I couldn't import com.squareup.okhttp3 ( package com.squareup does not exist). Why? I am new to the Java language itself and all the IDEs and tools that support it.

Diego Alves
  • 2,462
  • 3
  • 32
  • 65

3 Answers3

5

The import is just okhttp3: "import okhttp3" without com.square.

Diego Alves
  • 2,462
  • 3
  • 32
  • 65
3

Okhttp3 does not exist is due to a bug in Intellij IDE; however, there is a get-around:

  1. Place your 'com.squareup.okhttp3' dependency block at the end of the 'dependencies' list in the pom.xml file.
  2. 'import okhttp3...' is the right thing to to in your Java file.

pom.xml: 

        ...
        <dependency>
           <groupId>com.squareup.okhttp3</groupId>
           <artifactId>okhttp</artifactId>
           <version>4.5.0</version>
        </dependency>
   </dependencies>

Java file:

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
david m lee
  • 2,547
  • 26
  • 14
1

The correct coordinates for okhttp3 are:

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.9.1</version>
</dependency>

Note that the groupId has changed from com.squareup.okhttp to com.squareup.okhttp3.

So, if you update your pom.xml, replacing what you had for okhttp with what I posted above then you'll be able to resolve the okhttp classes.

For future reference you can find the okhttp artifacts on Maven Central.

glytching
  • 44,936
  • 9
  • 114
  • 120