Reborn
Reborn

Reputation: 37

Non-body HTTP method cannot contain @Body (Android)

I am new to android app, i am working on a project and i was testing Retrofit with get and i have error which gives error with Non-body HTTP method cannot contain @Body. There are many example in the website, i tried it with my code and it doesn't work.

public interface UserService {

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

i have seen many example for this error, i tried it and it still gives the error.

The code which i have tried:

public interface UserService {

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

my 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);
            }
        }

mainactivity:

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();
            }
        });

Upvotes: 3

Views: 4858

Answers (2)

Rakesh Saini
Rakesh Saini

Reputation: 763

It should be like below:

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

There is no need to add @Body

You can see more options from here

Upvotes: 1

jayesh gurudayalani
jayesh gurudayalani

Reputation: 1701

Non-body HTTP method cannot contain @Body This error basically you face when you try to pass request body to api by GET or DELETE method .. Before you write your code to call api , you need to first verify about api like end point url , request parameter , api method (GET , POST , PUT , DELETE etc .. ) .

If you think your api method is GET then you should make change like this

public interface UserService {

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

As you haven't mentioned api details , I assume from your code that you need to pass username as request parameter . So for GET you can do like this

If api method is POST then you can do like this

public interface UserService {

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

here LoginRequest object will be converted to JSON and sent to server but make sure you use some converter factory with retrofit like GsonConverterFactory

To learn about retrofit and GsonConverterFactory , follow this link

https://square.github.io/retrofit/

https://github.com/square/retrofit/tree/master/retrofit-converters/gson

Upvotes: 5

Related Questions