简体   繁体   English

如何使用Retrofit2进行GET请求?

[英]how to do a GET request using retrofit2?

I have a restfull web service which is running on a localhost. 我有一个在本地主机上运行的restfull Web服务。 I would like to make a retrofit2 GET request on that rest URL. 我想在该其余URL上进行一个retrofit2 GET请求。

MainActivity.java MainActivity.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 interface: GetUserListAPI.java接口:

public interface GetUserListAPI {
    @GET("/users")
    Call<List<UserPojo>> getUsersList();
}

When I make a call to requestData() method I keep getting response.code() as 404. 当我调用requestData()方法时,我一直将response.code()设为404。

Can anyone help me where should I have mistaken? 谁能帮我在哪里弄错了?

Thanks & Regards. 感谢和问候。

Your baseurl contains /users in the end.It should be removed as it will be received by the app from the inerface @GET("/users") Your baseUrl should be 您的baseurl末尾包含/ users。应将其删除,因为它会被应用程序从@GET(“ / users”)界面接收。您的baseUrl应该为

public static final String BASE_URL = "http://192.168.0.103:8080/SpringWithHibernate/"

and in interface 和在界面

public interface GetUserListAPI {
    @GET("users")
    Call<List<UserPojo>> getUsersList();
}

If you are getting 404 error its not associated with the retrofit, its backend issue. 如果出现404错误,则错误与改造无关,即后端问题。 Is that webservice is currently active one? 该Web服务当前处于活动状态吗? But i found another issue in your request, which is that 但是我在您的请求中发现了另一个问题,那就是

public static final String BASE_URL = "http://192.168.0.103:8080/SpringWithHibernate/users/";

and in Retrofit interface you have declared 在翻新界面中,您已经声明了

@GET("/users")

So the combined URL will result in 因此,合并的URL将导致

http://192.168.0.103:8080/SpringWithHibernate/users/users

Which is not correct, you should only declare your base url in the string and service names in retrofit interface methods. 这是不正确的,您只应在改造接口方法中的字符串和服务名称中声明您的基本URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM