简体   繁体   English

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

[英]java.lang.IllegalArgumentException: FormUrlEncoded can only be specified on HTTP methods with request body (e.g., @POST)

I am trying to get data from database by GET method on API我正在尝试通过 API 上的 GET 方法从数据库中获取数据

Here is my coding这是我的编码

APIServive.Interface APIServive.Interface

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

EventModel.Java事件模型.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视图活动.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) {

        }
    });}}

And When i run the application it gives this当我运行应用程序时,它给出了这个

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)

I've searched for this problems and i've tried to solve it, but until now i didnt got the right answers for this problem, if possible you can contact me cause i really need help我已经搜索过这个问题并试图解决它,但直到现在我还没有得到这个问题的正确答案,如果可能的话,你可以联系我,因为我真的需要帮助

Please Anybody help me and save my life thanks :')请任何人帮助我并挽救我的生命,谢谢:')

If you add @FromUrlEncoded to the top of @GET, you will have java.lang.IllegalArgumentException: FormUrlEncoded can only be specified on HTTP methods with request body (eg, @POST).如果将@FromUrlEncoded 添加到@GET 的顶部,则会出现java.lang.IllegalArgumentException: FormUrlEncoded can only be specified on HTTP methods with request body (eg, @POST).

Remove @FormUrlEncoded in your code .删除代码中的@FormUrlEncoded

public interface APIService {

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

If you replace @POST request with @GET , change also parameters and remove @FormUrlEncoded :如果您要更换@POST与要求@GET ,也改变参数和删除@FormUrlEncoded

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalArgumentException: HTTP 方法注释是必需的(例如,@GET、@POST 等)。尝试调用apiInterface GET 请求时出错 - java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., @GET, @POST, etc.).Error when trying call apiInterface GET request java.lang.IllegalArgumentException:响应必须包括泛型类型(例如,响应<string> )</string> - java.lang.IllegalArgumentException: Response must include generic type (e.g., Response<String>) java.lang.IllegalArgumentException:只能指定目标位置 - java.lang.IllegalArgumentException: Only the target location may be specified java.lang.IllegalArgumentException:未指定Bean - java.lang.IllegalArgumentException: No bean specified 注册后发布方法-java.lang.IllegalArgumentException - On register post method - java.lang.IllegalArgumentException java.lang.IllegalArgumentException - java.lang.IllegalArgumentException java.lang.IllegalArgumentException - java.lang.IllegalArgumentException java.lang.IllegalArgumentException:未指定Junit和Mockito的数据源 - java.lang.IllegalArgumentException: No DataSource specified Junit and Mockito 骆驼:java.lang.IllegalArgumentException:必须指定defaultEndpoint - Camel: java.lang.IllegalArgumentException: defaultEndpoint must be specified 使用Spring Security,我如何使用HTTP方法(例如GET,PUT,POST)来确定特定URL模式的安全性? - Using Spring Security, how can I use HTTP methods (e.g. GET, PUT, POST) to distingush security for particular URL patterns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM