2

I am using postman extension for post request. I want to make same request with android. I used retrofit library for access my goal. But I can't get successful result. Where is my mistake in code ?

Postman : enter image description here

My interface :

public interface Interfacem {

    @FormUrlEncoded
    @POST("/login")
    Call<ResponseBody> getResponse(@Field("signin[username]") String username,@Field("signin[password]")String password );
}

and retrofit usage :

Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://myurl.com")
                        .build();

                Interfacem service = retrofit.create(Interfacem.class);
                Call<ResponseBody> result =service.getResponse("myUsername","myPassword");
                result.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Response<ResponseBody> response) {
                        try {
                            System.out.println(response.body().string().toString());
                        } catch (IOException|NullPointerException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(Throwable t) {
                        t.printStackTrace();
                    }
                });
yonder
  • 111
  • 1
  • 9

3 Answers3

1

Try this provide body

public class SignBody {
  @SerializedName("signin[username]") @Expose private String username;
  @SerializedName("signin[password]") @Expose private String password;
}

change interface to

@POST("/login")
    Call<ResponseBody> getResponse(@Body SignBody body);
Yarh
  • 4,459
  • 5
  • 45
  • 95
1

if you are using Retrofit 2.x, Try to change your build of Retrofit object as below :

Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://myurl.com/")
                        .build();

maybe the things below will help

the leading / within the partial url overrides the end of API endpoint definition. Removing the / from the partial url and adding it to the base url will bring the expected result.

Example:

The API interface

    public interface UserService {  
    @POST("/me")
    Call<User> me();}

Build Retrofit with baseUrl

    Retrofit retrofit = Retrofit.Builder()  
    .baseUrl("https://your.api.url/v2");
    .build();

Then Call :

UserService service = retrofit.create(UserService.class);

--> The request Url will be https://your.api.url/me (/v2 has been disapear)

PRO TIP

use relative urls for your partial endpoint urls and end your base url with the trailing slash /.

Interface

public interface UserService {  
    @POST("me")
    Call<User>me();
}

Retrofit with baseURL

Retrofit retrofit = Retrofit.Builder()  
    .baseUrl("https://your.api.url/v2/");
    .build();

Call

UserService service = retrofit.create(UserService.class);

--> The request URL will be https://your.api.url/v2/me

ThaiPD
  • 3,503
  • 3
  • 30
  • 48
0

Which version you use in retrofit? Below code for version 2.0. If any Header require please check. If you can share url and username or password. I will check.

public interface Interfacem {

    @FormUrlEncoded
    @POST("login")
    Call<ResponseBody> getResponse(@Field("username") String username,@Field("password")String password );
}


Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://myurl.com/")
                        .build();

                Interfacem service = retrofit.create(Interfacem.class);
                Call<ResponseBody> result =service.getResponse("myUsername","myPassword");
                result.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Response<ResponseBody> response) {
                        try {
                            System.out.println(response.body().string().toString());
                        } catch (IOException|NullPointerException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(Throwable t) {
                        t.printStackTrace();
                    }
                });
Kaushal Patel
  • 338
  • 4
  • 19