Search code examples
springjparetrofitretrofit2

retrofit spring boot not responding


I have a server in spring boot, which is running on port 8080.

Now I'm trying to call rest api in my android application using retrofit2.

Here is what I have implemented:

        final TextView textView=findViewById(R.id.t1);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://localhost:8080/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);

        Call<TC> call = jsonPlaceHolderApi.getPosts();

        call.enqueue(new Callback<TC>() {
            @Override
            public void onResponse(Call<TC> call, Response<TC> response) {
                textView.setText(response.toString());

                if (!response.isSuccessful()) {
                    return;
                }

                TC posts = response.body();
                textView.setText(posts.toString());
            }

            @Override
            public void onFailure(Call<TC> call, Throwable t) {

            }
        });

I can surly say that, it's not working as my api is not even being called. As the hello world screen remains as it is.

And in my server I have logger, which doesn't log anything, so it doesn't get called.

Here is my CORS:

@Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry
                        .addMapping("*")
                        .allowedOrigins("*");
            }
        };
    }

Solution

  • The problem is with the word localhost.

    As for debugging purpose I'm connecting the android device with my PC so my android device can't connect to localhost as it is just an alias of my IP address.

    localhost equivalent

    To resolve this, I opened up CMD and wrote ipconfig, by this command I can see all details related to IP.

    And here it shows my IPv4 address as $.$.$.$. I just replaced the localhost with that $.$.$.$. Now everything is working fine.