简体   繁体   中英

java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., @GET, @POST, etc.).Error when trying call apiInterface GET request

I get error when trying to call API interface get request. I am trying to pass a value through GET method. But I get this error in logcat Caused by: java.lang.IllegalArgumentException: HTTP method annotation is required (eg, @GET, @POST, etc.). for method ApiInterface.name Caused by: java.lang.IllegalArgumentException: HTTP method annotation is required (eg, @GET, @POST, etc.). for method ApiInterface.name

Here is my java code. Here I call the function namely getname() to pass a string value through GET request. Here I also have one more function getdata() which display output in the recyclerview.

public class MainActivity extends AppCompatActivity {



    RecyclerView recyclerView;
    ListAdapter1 listAdapter;
//    List<SupermarketModels> supermarketModelsList = new ArrayList<>();
    ApiInterface apiInterface;
    String hi;
    Button badd;

    EmptyAdapterl emptyAdapter = new EmptyAdapterl();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        badd=findViewById(R.id.btadd);

        badd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent= new Intent(MainActivity.this,Insert.class);
                startActivity(intent);

            }
        });




        String tablen= getIntent().getStringExtra("name");



//        supermarketModelsList.add(new SupermarketModels(1,"sugar cane","20kg",
//                "50rs","31/12/2021"));
//
//        supermarketModelsList.add(new SupermarketModels(2,"sugar cane","20kg",
//                "50rs","31/12/2021"));
//        supermarketModelsList.add(new SupermarketModels(3 ,"sugar cane","20kg",
//                "50rs","31/12/2021"));
        initialization();


        hi=tablen;

        Toast.makeText(MainActivity.this,"oo"+hi,Toast.LENGTH_SHORT).show();

        getname(hi);




//        recyclerView.setAdapter(emptyAdapter);



//        getdata();




    }

    private void initialization(){
        recyclerView = findViewById(R.id.recyclerview1);
        Retrofit retrofit = APIClient.getclient();
        apiInterface = retrofit.create(ApiInterface.class);
//        apiInterface = APIClient.getRetrofitInstance().create(ApiInterface.class);


    }

    private void setadapter(List<SupermarketModels> supermarketModels){


        listAdapter = new ListAdapter1(this, supermarketModels,hi);

//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        @SuppressLint("WrongConstant")
        RecyclerView.LayoutManager lm = new LinearLayoutManager(getApplicationContext(), LinearLayout.VERTICAL, false);
        recyclerView.setLayoutManager(lm);

//        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setHasFixedSize(true);

        recyclerView.setLayoutManager(lm);



        recyclerView.setAdapter(listAdapter);





    }

    private void getname(String n) {

        apiInterface.name(n).enqueue(new Callback<DeleteResponse>() {
            @Override
            public void onResponse(Call<DeleteResponse> call, Response<DeleteResponse> response) {

                try {
                    if (response.body().getStatus().equals("1")){
                        Toast.makeText(MainActivity.this,"Successfully inserted",Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Toast.makeText(MainActivity.this,"Error while inserting",Toast.LENGTH_SHORT).show();
                    }

                } catch (Exception e){

                }

            }

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

            }
        });




    }

    private void getdata(){
        apiInterface.getList().enqueue(new Callback<GetListResponse>() {
            @Override
            public void onResponse(Call<GetListResponse> call, Response<GetListResponse> response) {

                try {
                    if (response!= null){
                        if (response.body().getStatus().equals("1")){

//                            category_adapter category_adapter = new category_adapter(getContext(),datalist);



                            setadapter(response.body().getData());


                        }


                        else {
                            Toast.makeText(MainActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (Exception e){
                    Log.e("exp", e.getLocalizedMessage());

                }

            }

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

            }
        });

    }
}

This is my apiInterface class


 public interface ApiInterface {

    @GET("user-controller.php?operation=list")
    Call<GetListResponse> getList();
    Call<DeleteResponse> name(@Field("name") String name1);
} 

make sure you are using Retrofit2 in your @GET Imports (maybe you are using the wrong @Get)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM