简体   繁体   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

I get error when trying to call API interface get request.尝试调用 API 接口获取请求时出错。 I am trying to pass a value through GET method.我试图通过 GET 方法传递一个值。 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但我在 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.这是我的 java 代码。 Here I call the function namely getname() to pass a string value through GET request.这里我调用 function 即getname()通过 GET 请求传递一个字符串值。 Here I also have one more function getdata() which display output in the recyclerview.在这里,我还有一个 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) {

            }
        });

    }
}

This is my apiInterface class这是我的 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)确保您在@GET 导入中使用了 Retrofit2(也许您使用了错误的@Get)

暂无
暂无

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

相关问题 Android Retrofit 错误 HTTP 方法注释是必需的(例如,@GET、@POST 等) - Android Retrofit error HTTP method annotation is required (e.g., @GET, @POST, etc.) Retrofi2 Android HTTP 方法注释是必需的(例如,@GET,@POST等) - Retrofi2 Android HTTP method annotation is required (e.g., @GET, @POST, etc.) Android Studio 显示此错误:Retrofit2 Android HTTP 方法注释是必需的(例如,@GET、@POST 等) - Android Studio showing this error :Retrofit2 Android HTTP method annotation is required (e.g., @GET, @POST, etc.) java.lang.IllegalArgumentException:FormUrlEncoded 只能在带有请求正文的 HTTP 方法上指定(例如,@POST) - java.lang.IllegalArgumentException: FormUrlEncoded can only be specified on HTTP methods with request body (e.g., @POST) java.lang.IllegalArgumentException:响应必须包括泛型类型(例如,响应<string> )</string> - java.lang.IllegalArgumentException: Response must include generic type (e.g., Response<String>) 注册后发布方法-java.lang.IllegalArgumentException - On register post method - java.lang.IllegalArgumentException 为什么我得到java.lang.IllegalArgumentException:XPath为null时找不到元素错误消息 - Why do I get java.lang.IllegalArgumentException: Cannot find elements when the XPath is null error message java.lang.IllegalArgumentException:error引用的类型不是注释类型 - java.lang.IllegalArgumentException: error Type referred to is not an annotation type Method getResources没有用请求类型注释(例如,GET,POST)是什么意思? - what does Method getResources not annotated with request type (e.g., GET, POST) mean? java.lang.IllegalArgumentException:启动tomcat时需要&#39;dataSource&#39;或&#39;jdbcTemplate&#39; - java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required when starting tomcat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM