[英]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.