我有一个在本地主机上运行的restfull web服务。我想对那个rest url提出一个改进的get请求。
main活动.java
private void requestData() {
public static final String BASE_URL = "http://192.168.0.103:8080/SpringWithHibernate/users/";
Toast.makeText(MainActivity.this, "In requestData() :: " + "ddd", Toast.LENGTH_LONG).show();
Gson gson = new GsonBuilder().create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
GetUserListAPI api = retrofit.create(GetUserListAPI.class);
api.getUsersList().enqueue(new Callback<List<UserPojo>>() {
@Override
public void onResponse(Call<List<UserPojo>> call, Response<List<UserPojo>> response) {
Log.d("UserList :: ", "Success");
Log.d("UserList :: ", "Code :: " + response.code());
user = response.body();
// Log.d("UserList :: ", "count :: " + user.size());
// Log.d("UserList :: ", "Result :: " + user.get(1).getUsername());
}
@Override
public void onFailure(Call<List<UserPojo>> call, Throwable t) {
Log.d("UserList :: ", "Failure");
}
});
}
getuserlistapi.java接口:
public interface GetUserListAPI {
@GET("/users")
Call<List<UserPojo>> getUsersList();
}
当我调用requestData()方法时,我得到的response.code()是404。
有人能帮我把我错在哪里吗?
谢谢和问候。
最佳答案
你的baseurl最后包含/users。应该删除它,因为应用程序将从inerface@get(“/users”)接收它
你的baseurl应该是
public static final String BASE_URL = "http://192.168.0.103:8080/SpringWithHibernate/"
在界面上
public interface GetUserListAPI {
@GET("users")
Call<List<UserPojo>> getUsersList();
}