I am trying to get current weather from an API, and for some reason, when I am trying to create a Retrofit object, it returns me an error:
Process: com.api.weather_api_training, PID: 8417
java.lang.NoSuchMethodError: No static method metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; in class Ljava/lang/invoke/LambdaMetafactory; or its super classes (declaration of 'java.lang.invoke.LambdaMetafactory' appears in /apex/com.android.runtime/javalib/core-oj.jar)
at okhttp3.internal.Util.<clinit>(Util.java:87)
at okhttp3.internal.Util.skipLeadingAsciiWhitespace(Util.java:321)
at okhttp3.HttpUrl$Builder.parse(HttpUrl.java:1313)
at okhttp3.HttpUrl.get(HttpUrl.java:917)
at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:506)
at com.api.weather_api_training.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Here is my build.gradle file:
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.api.weather_api_training"
minSdkVersion 25
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
}
MainActivity:
package com.api.weather_api_training;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import android.os.Bundle;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView temperatureTextView;
private TextView humidityTextView;
private TextView windSpeedTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
temperatureTextView = findViewById(R.id.temperatureText);
humidityTextView = findViewById(R.id.humidityText);
windSpeedTextView = findViewById(R.id.windSpeedText);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.openweathermap.org/") // Error is here
.addConverterFactory(GsonConverterFactory.create())
.build();
WeatherServiceAPI weatherServiceAPI = retrofit.create(WeatherServiceAPI.class);
Call<List<WeatherClass>> weatherCall = weatherServiceAPI.getWeather();
weatherCall.enqueue(new Callback<List<WeatherClass>>() {
@Override
public void onResponse(Call<List<WeatherClass>> call, Response<List<WeatherClass>> response) {
if(response.isSuccessful()){
WeatherClass weatherClass = (WeatherClass) response.body();
System.out.println(weatherClass.getCurrentWeather().getHumidity());
}
}
@Override
public void onFailure(Call<List<WeatherClass>> call, Throwable t) {
}
});
}
}
WeatherServiceAPI:
package com.api.weather_api_training;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface WeatherServiceAPI {
@GET("data/2.5/onecall?lat=50.0412&lon=21.9991&exclude=hourly,daily&appid=(my id)")
Call<List<WeatherClass>> getWeather();
}
Of course, in (my id), I put in my id from the website. Is there any way to fix that error? Thanks in advance.
Based on this answer you need to switch to Java 8 compatibility
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With