好的,所以我正在制作一个电影应用程序,而这样做,我遇到了这个问题。我有这个函数,当按下时将打印出一些电影的标题,但当我尝试运行它时,我得到这个错误消息,我正在使用TMDB API获取电影。如果你知道怎么修理它,请告诉我,谢谢!
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.IllegalArgumentException: Missing either @GET URL or @Url parameter.
for method MovieApi.searchMovie
at retrofit2.Utils.methodError(Utils.java:54)
at retrofit2.Utils.methodError(Utils.java:43)
at retrofit2.RequestFactory$Builder.build(RequestFactory.java:210)
at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:67)
at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:26)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:202)
at retrofit2.Retrofit$1.invoke(Retrofit.java:160)
at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
at $Proxy1.searchMovie(Unknown Source)
at com.example.watchitmovieapp.MainActivity.GetRetrofitResponse(MainActivity.java:44)
at com.example.watchitmovieapp.MainActivity.onButtonClicked(MainActivity.java:37)
这是我的代码<<strong>主要活动/strong>package com.example.watchitmovieapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.example.watchitmovieapp.models.MovieModel;
import com.example.watchitmovieapp.request.Servicey;
import com.example.watchitmovieapp.response.MovieSearchResponse;
import com.example.watchitmovieapp.utils.Credentials;
import com.example.watchitmovieapp.utils.MovieApi;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.button);
}
public void onButtonClicked(View view){
GetRetrofitResponse();
}
private void GetRetrofitResponse() {
MovieApi movieApi = Servicey.getMovieApi();
Call<MovieSearchResponse> responseCall = movieApi
.searchMovie(
Credentials.API_KEY,
"Jack Reacher",
"1");
responseCall.enqueue(new Callback<MovieSearchResponse>() {
@Override
public void onResponse(Call<MovieSearchResponse> call, Response<MovieSearchResponse> response) {
if (response.code() == 200){
//Log.v("Tag", "The response" + response.body().toString());
List<MovieModel> movies = new ArrayList<>(response.body().getMovies());
for (MovieModel movie: movies){
Log.v("Tag", "Name" + movie.getTitle());
}
}else{
try {
Log.v("Tag", "Error" + response.errorBody().string());
}catch (IOException e){
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<MovieSearchResponse> call, Throwable t) {
}
});
}
}
Servicey
package com.example.watchitmovieapp.request;
import com.example.watchitmovieapp.utils.Credentials;
import com.example.watchitmovieapp.utils.MovieApi;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
public class Servicey {
private static Retrofit.Builder retrofitBuilder =
new Retrofit.Builder()
.baseUrl(Credentials.BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
private static Retrofit retrofit = retrofitBuilder.build();
private static MovieApi movieApi = retrofit.create(MovieApi.class);
public static MovieApi getMovieApi(){
return movieApi;
}
}
MovieSearchResponse
package com.example.watchitmovieapp.response;
import com.example.watchitmovieapp.models.MovieModel;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class MovieSearchResponse {
@SerializedName("total_results")
@Expose()
private int total_count;
@SerializedName("results")
@Expose()
private List<MovieModel> movies;
public int getTotal_count(){
return total_count;
}
public List<MovieModel> getMovies(){
return movies;
}
@Override
public String toString() {
return "MovieSearchResponse{" +
"total_count=" + total_count +
", movies=" + movies +
'}';
}
}
MovieApi
package com.example.watchitmovieapp.utils;
import android.graphics.Movie;
import com.example.watchitmovieapp.response.MovieSearchResponse;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface MovieApi {
// searching
@GET
Call<MovieSearchResponse> searchMovie(
@Query("key") String key,
@Query("query") String query,
@Query("page") String page
);
}
电影
package com.example.watchitmovieapp.models;
import android.os.Parcel;
import android.os.Parcelable;
public class MovieModel implements Parcelable {
private String title;
private String poster_path;
private String release_date;
private int movie_id;
private float vote_average;
private String movie_overview;
//Constructor
public MovieModel(String title, String poster_path, String release_date, int movie_id, float vote_average, String movie_overview) {
this.title = title;
this.poster_path = poster_path;
this.release_date = release_date;
this.movie_id = movie_id;
this.vote_average = vote_average;
this.movie_overview = movie_overview;
}
//Getters
protected MovieModel(Parcel in) {
title = in.readString();
poster_path = in.readString();
release_date = in.readString();
movie_id = in.readInt();
vote_average = in.readFloat();
movie_overview = in.readString();
}
public static final Creator<MovieModel> CREATOR = new Creator<MovieModel>() {
@Override
public MovieModel createFromParcel(Parcel in) {
return new MovieModel(in);
}
@Override
public MovieModel[] newArray(int size) {
return new MovieModel[size];
}
};
public String getTitle() {
return title;
}
public String getPoster_path() {
return poster_path;
}
public String getRelease_date() {
return release_date;
}
public int getMovie_id() {
return movie_id;
}
public float getVote_average() {
return vote_average;
}
public String getMovie_overview() {
return movie_overview;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(title);
parcel.writeString(poster_path);
parcel.writeString(release_date);
parcel.writeInt(movie_id);
parcel.writeFloat(vote_average);
parcel.writeString(movie_overview);
}
}
在MovieApi interface
的searchMovie()
方法中@GET
之后缺少URL(请求目标路径)
的例子:@GET("api/fruit/all")