返回

android-java.lang.IllegalArgumentException:需要HTTP方法注释(例如,@GET、@POST等)。尝试调用apiInterface GET请求时出错

发布时间:2022-05-13 04:50:09 314
# java

我在尝试调用API接口get请求时出错。我试图通过GET方法传递一个值。但我在logcat中发现了这个错误 Caused by: java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., @GET, @POST, etc.). for method ApiInterface.name

这是我的java代码。这里我称之为函数getname()通过GET请求传递字符串值。这里我还有一个功能getdata()在recyclerview中显示输出。

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    ListAdapter1 listAdapter;
//    List 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){


        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() {
            @Override
            public void onResponse(Call call, Response 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 call, Throwable t) {

            }
        });




    }

    private void getdata(){
        apiInterface.getList().enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response 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 call, Throwable t) {

            }
        });

    }
}

这是我的API接口类


 public interface ApiInterface {

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

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像
下一篇
TypeScript:检查变量是否已初始化 2022-05-13 03:35:17