在 Android 应用程序中使用 Retrofit Builder 时出错:java.lang.NoSuchMetho



我正在尝试从 API 获取当前天气,出于某种原因,当我尝试创建 Retrofit 对象时,它返回了一个错误:

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)

这是我的build.gradle文件:


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'

}

主要活动:

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) {
}
});
}
}

天气服务API:

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();
}

当然,在(我的ID(中,我从网站上输入了我的ID。 有没有办法解决该错误?提前谢谢。

基于这个答案,你需要切换到Java 8兼容性

android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

相关内容

  • 没有找到相关文章

最新更新