java.lang.IllegalArgumentException:FormUrlEncoded只能在具有请求正文的HTTP方法上指定(例如,@POST)

2022-09-04 07:56:08

我正在尝试通过API上的GET方法从数据库中获取数据

这是我的编码

APIServive.Interface

public interface APIService {
   @FormUrlEncoded
   @GET("Event")
   Call<ApiResponseModel> viewEvent();
}

EventModel.Java

 public class EventModel {

    @SerializedName("nama_event") String nama_event;
    @SerializedName("jenis_event") String jenis_event;
    @SerializedName("creator") String creator;
    @SerializedName("deskripsi_event") String deskripsi_event;
    @SerializedName("tanggal") String tanggal;
    @SerializedName("status") String status;

    public String getNama_event() {
        return nama_event;
    }

    public String getJenis_event() {
        return jenis_event;
    }

    public String getCreator() {
        return creator;
    }

    public String getDesk_event() {
        return deskripsi_event;
    }

    public String getTanggal_event() {
        return tanggal;
    }

    public String getStatus() {
        return status;
    }
}

ViewActivity.Java

 public class ViewEventActivity extends AppCompatActivity {
    @OnClick(R.id.back_arrow)void balik(){
    finish();
}
    @BindView(R.id.search_acara)EditText searchEvent;
    public static final String URL = "http://iseoo.id/rest_ci_iseoo/";
    private List<EventModel> acara = new ArrayList<>();
       RecyclerView.LayoutManager mlayoutManager;
       private RecyclerViewAdapter viewAdapter;
       @BindView(R.id.recyclerViewEvent)RecyclerView recyclerView;
       @BindView(R.id.progress_bar)ProgressBar progressBar;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_view_event);
      ButterKnife.bind(this);

      viewAdapter = new RecyclerViewAdapter(ViewEventActivity.this, acara);
      mlayoutManager = new LinearLayoutManager(this, 
      LinearLayoutManager.VERTICAL, false);
      recyclerView.setLayoutManager(mlayoutManager);
      recyclerView.setAdapter(viewAdapter);
      Retrofit retrofit=new 
      Retrofit.Builder().baseUrl(URL).
      addConverterFactory(GsonConverterFactory.create()).build();
      APIService API = retrofit.create(APIService.class);
      Call<ApiResponseModel> getData = API.viewEvent();
      getData.enqueue(new Callback<ApiResponseModel>() {
        @Override
        public void onResponse(Call<ApiResponseModel> call, 
        Response<ApiResponseModel> response) {
        progressBar.setVisibility(View.GONE);
                acara = response.body().getResult();
                viewAdapter = new 
                RecyclerViewAdapter(ViewEventActivity.this, acara);
                recyclerView.setAdapter(viewAdapter);
                viewAdapter.notifyDataSetChanged();
            }
         //        }

        @Override
        public void onFailure(Call<ApiResponseModel> call, Throwable t) {

        }
    });}}

当我运行应用程序时,它会给出这个

Caused by: java.lang.IllegalArgumentException: FormUrlEncoded can only be 
specified on HTTP methods with request body (e.g., @POST).                                                                                   
for method APIService.viewEvent                                                                                   
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:752)                                                                                   
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:743)                                                                                   
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:185)                                                                                   
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)                                                                                   
at retrofit2.Retrofit$1.invoke(Retrofit.java:147)                                                                                   
at java.lang.reflect.Proxy.invoke(Proxy.java:397)                                                                                   
at $Proxy0.viewEvent(Unknown Source)                                                                                   
at 
com.example.lenovog480.iseooalpha.ViewEventActivity.onCreate
(ViewEventActivity.java:61)                                                                                   
at android.app.Activity.performCreate(Activity.java:6127)                                                                                   
at android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1128)                                                                                   
at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2630)

我已经搜索了这个问题,我试图解决它,但直到现在我还没有得到这个问题的正确答案,如果可能的话,你可以联系我,因为我真的需要帮助

请任何人帮助我,救我一命,谢谢:')


答案 1

如果将@FromUrlEncoded添加到@GET的顶部,您将获得java.lang.IllegalArgumentException: FormUrlEncoded can only be specified on HTTP methods with request body (e.g., @POST).

在代码中删除 。@FormUrlEncoded

public interface APIService {

   @GET("Event")
   Call<ApiResponseModel> viewEvent();
}

答案 2

如果将请求替换为 ,则也更改参数并删除:@POST@GET@FormUrlEncoded

@GET(/some/request/)
suspend fun getData(
    @Query("name") name: String?,
    @Query("age") age: Int?
): SomeResponse

推荐