非正文HTTP方法不能包含@Body(Android)

dm7nw8vv  于 2023-05-05  发布在  Android
关注(0)|答案(2)|浏览(329)

我是新的Android应用程序,我正在做一个项目,我正在用get测试Retrofit,我有错误,它给出了错误非正文HTTP方法不能包含@Body。有很多例子在网站上,我尝试了我的代码,它不工作。

public interface UserService {

    @GET("person/")
    Call<LoginResponse> loginUser(@Body LoginRequest loginRequest);
}

我见过很多这个错误的例子,我试过了,它仍然给出了错误。
我试过的代码:

public interface UserService {

    @GET("person/")
    Call<LoginResponse> loginUser(@Query("username") LoginRequest loginRequest);
}

我的API

public class UserController : ApiController
    {
        public SqlConnection mainconn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString);

        [HttpGet]
        public IHttpActionResult GetUsers()
        {
            List<People> list = new List<People>();
            string sqlquery = "Select LoginID, PersonEye, username, password from tblLogin";
            using (SqlCommand com = new SqlCommand(sqlquery, mainconn))
            {
                mainconn.Open();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    list.Add(new People()
                    {
                        LoginID = Convert.ToInt32(dr.GetValue(0)),
                        PersonEye = Convert.ToString(dr.GetValue(1)),
                        username = Convert.ToString(dr.GetValue(2)),
                        password = Convert.ToString(dr.GetValue(3))
                    });
                }
            }
            return Ok(list);
        }

        [HttpGet]
        public IHttpActionResult GetuserName(string username)
        {
            List<People> list = new List<People>();
            using (SqlCommand com = new SqlCommand("Select LoginID, PersonEye, username, password from tblLogin where charindex(@name, username) = 1", mainconn))
            {
                mainconn.Open();
                com.Parameters.Add("@name", SqlDbType.VarChar);
                com.Parameters["@name"].Value = username.ToString();
                using (SqlDataReader dr = com.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        list.Add(new People()
                        {
                            LoginID = Convert.ToInt32(dr.GetValue(0)),
                            PersonEye = Convert.ToString(dr.GetValue(1)),
                            username = Convert.ToString(dr.GetValue(2)),
                            password = Convert.ToString(dr.GetValue(3))
                        });
                    }
                }
                return Ok(list);
            }
        }

主要活动:

public void loginUser(LoginRequest loginRequest) {
        Call<LoginResponse> loginResponseCall = ApiClient.getService().loginUser(); //<- here have error after change
        loginResponseCall.enqueue(new Callback<LoginResponse>() {
            @Override
            public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
                if(response.isSuccessful()) {
                    LoginResponse loginResponse = response.body();
                    startActivity(new Intent(MainActivity.this, Visitor.class).putExtra("data",loginResponse));
                    finish();
                } else {
                    String message = "An error occurred please try again later... ";
                    Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<LoginResponse> call, Throwable t) {
                String message = t.getLocalizedMessage();
                Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
            }
        });
mhd8tkvw

mhd8tkvw1#

Non-body HTTP method cannot contain @Body当你尝试通过GET或DELETE方法将请求体传递给API时,你会遇到这个错误。在编写调用API的代码之前,您需要首先验证api,如端点url,请求参数,api方法(GET,POST,PUT,DELETE等)。)的。
如果你认为你的API方法是GET,那么你应该像这样做改变

public interface UserService {

    @GET("person/")
    Call<LoginResponse> loginUser(@Query("username") String username);
}

由于你没有提到API细节,我假设你的代码需要传递username作为请求参数。对于GET,你可以这样做
如果API方法是POST,那么你可以这样做

public interface UserService {

    @POST("person/")
    Call<LoginResponse> loginUser(@Body LoginRequest loginRequest);
}

这里LoginRequest对象将被转换为JSON并发送到服务器,但请确保使用一些具有改进功能的转换器工厂,如GsonConverterFactory
要了解retrofitGsonConverterFactory,请访问此链接
https://square.github.io/retrofit/
https://github.com/square/retrofit/tree/master/retrofit-converters/gson

ssgvzors

ssgvzors2#

它应该像下面这样:

@GET("person/")
Call<LoginResponse> loginUser();

不需要添加@Body
您可以从here中看到更多选项

相关问题