I am using Retrofit2. I have created an API using Yii2 framework. I am targeting Base URL - http://192.168.0.105/rest-root/web/index.php?r= and my end-point is "root/register"
My code in activity:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.105/rest-root/web/index.php?r=/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RegisterDetails registerDetails = new RegisterDetails(email, mobile, password);
// Creating a web service call..
RegisterAPIService registerAPIService = retrofit.create(RegisterAPIService.class);
Call<RegisterModel> call =registerAPIService.registerUser(registerDetails);
call.enqueue(new Callback<RegisterModel>() {
@Override
public void onResponse(Response<RegisterModel> response) {
Log.d("Message", "code..."+response.code() + " message..." + response.message()+" body..."+response.body());
}
@Override
public void onFailure(Throwable t) {
}
});
} else {
Log.d("Message", "Something went wrong");
}
My API interface:
public interface RegisterAPIService {
@POST("/register")
Call<RegisterModel> registerUser(@Body RegisterDetails registerDetails);
}
My Input data class(RegisterDetails.java):
public class RegisterDetails {
String email;
String password;
String mobile;
public RegisterDetails(String email, String mobile, String password) {
this.password = password;
this.email = email;
this.mobile = mobile;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
I simply dont understand why am I getting 404(Not found) for such a simple thing. FYI. I am using Yii2 for the first time with Retrofit. The web-service is running perfectly. I am able to access the URL from browser correctly.
It looks like you are confusing usage of baseUrl. Take a look in corresponding issue on StackOverFlow
In your case you need simply remove slash inside endpoint.
@POST("register")
Call<RegisterModel> registerUser(@Body RegisterDetails registerDetails);
The end result of your request is "http://192.168.0.105/register/" that is why you are experiencing 404.
Best regards!
Looks like your base url is incorrect, please double check that you're
requesting correct url.
You can debug or log request/response on both client and server side and
see what goes wrong.
On Thu, Nov 19, 2015, 09:32 surajgusain [email protected] wrote:
I am using Retrofit2. I have created an API using Yii2 framework. I am
targeting Base URL - http://192.168.0.105/rest-root/web/index.php?r= and
my end-point is "root/register"My code in activity:
Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://192.168.0.105/rest-root/web/index.php?r=/") .addConverterFactory(GsonConverterFactory.create()) .build(); RegisterDetails registerDetails = new RegisterDetails(email, mobile, password);// Creating a web service call..
RegisterAPIService registerAPIService = retrofit.create(RegisterAPIService.class); Call<RegisterModel> call =registerAPIService.registerUser(registerDetails); call.enqueue(new Callback<RegisterModel>() { @Override public void onResponse(Response<RegisterModel> response) { Log.d("Message", "code..."+response.code() + " message..." + response.message()+" body..."+response.body()); } @Override public void onFailure(Throwable t) { } }); } else { Log.d("Message", "Something went wrong"); }My API interface:
public interface RegisterAPIService {@POST("/register")
CallregisterUser(@Body RegisterDetails registerDetails); }
My Input data class(RegisterDetails.java):
public class RegisterDetails {
String email;
String password;
String mobile;public RegisterDetails(String email, String mobile, String password) {
this.password = password;
this.email = email;
this.mobile = mobile;
}public String getPassword() {
return password;
}public void setPassword(String password) {
this.password = password;
}public String getEmail() {
return email;
}public void setEmail(String email) {
this.email = email;
}public String getMobile() {
return mobile;
}public void setMobile(String mobile) {
this.mobile = mobile;
}}
I simply dont understand why am I getting 404(Not found) for such a simple
thing. FYI. I am using Yii2 for the first time with Retrofit. The
web-service is running perfectly. I am able to access the URL from browser
correctly.—
Reply to this email directly or view it on GitHub
https://github.com/square/retrofit/issues/1303.
@artem_zin
I reverted back to Retrofit v1.9 and everything is running despite of the URL remaining the same.
@surajgusain what URL do you want, and what URL are you getting?
@swankjesse The URL that I want to target is: http://192.168.0.105/root-web/web/index.php?r=root/register.
Use http://192.168.0.105/root-web/web/index.php as your base URL and ?r=root/register in the @GET value.
Here's proof it will behave correctly when used this way:
// http://192.168.0.105/root-web/web/index.php?r=root/register
HttpUrl baseUrl = HttpUrl.parse("http://192.168.0.105/root-web/web/index.php");
System.out.println("Base URL: " + baseUrl);
HttpUrl resolved = baseUrl.resolve("?r=root/register");
System.out.println("Resolved: " + resolved);
Base URL: http://192.168.0.105/root-web/web/index.php
Resolved: http://192.168.0.105/root-web/web/index.php?r=root/register
Try This:
Use Base url as:
.baseUrl("http://192.168.0.105")
And in you interface use the rest of the part of url as:
@POST("/rest-root/web/index.php?r=register")
@mushthak Thanks a lot you saved my day..
Initially
My URL was
String BASE_URL = "http://mydomain.com/rest/v1/abc/service/";
@POST("/service/save/98925")
Call
But then I changed it to,
String BASE_URL = "http://mydomian.com/";
@POST("rest/v1/abc/service/save/98925")
Call
I'm using this version compile 'com.squareup.retrofit2:retrofit:2.2.0'
I hope this would help others too.
Thanks
Okay, there is a lot of misinformation in this thread (largely because the question keeps changing), so if you're having trouble combining the BaseUrl with a relative/absolute path, look at this SO answer by JW, it explains it pretty well I think.
Most helpful comment
Try This:
Use Base url as:
.baseUrl("http://192.168.0.105")
And in you interface use the rest of the part of url as:
@POST("/rest-root/web/index.php?r=register")