簡體   English   中英

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

嘗試調用 API 接口獲取請求時出錯。 我試圖通過 GET 方法傳遞一個值。 但我在 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

這是我的 java 代碼。 這里我調用 function 即getname()通過 GET 請求傳遞一個字符串值。 在這里,我還有一個 function getdata() ,它在 recyclerview 中顯示 output。

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

            }
        });

    }
}

這是我的 apiInterface class


 public interface ApiInterface {

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

確保您在@GET 導入中使用了 Retrofit2(也許您使用了錯誤的@Get)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM