Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Here is my coding

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

        }
    });}}

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 :')

like image 525
Muhammad Haekal Avatar asked Oct 27 '17 00:10

Muhammad Haekal


2 Answers

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 (e.g., @POST).

Remove @FormUrlEncoded in your code .

public interface APIService {

   @GET("Event")
   Call<ApiResponseModel> viewEvent();
}
like image 60
KeLiuyue Avatar answered Nov 18 '22 22:11

KeLiuyue


If you replace @POST request with @GET, change also parameters and remove @FormUrlEncoded:

@GET(/some/request/)
suspend fun getData(
    @Query("name") name: String?,
    @Query("age") age: Int?
): SomeResponse
like image 22
CoolMind Avatar answered Nov 18 '22 22:11

CoolMind