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