-1

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("*");
            }
        };
    }
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
  • 1
    Do not ignore error messages you receive inside `onFailure()` method of Retrofit. Please log the error response `Log.e("ERROR", t);` and [edit] the question post error messages/stack trace here. – Shashanth Jan 22 '20 at 05:56
  • i resolved the problem, check out my answer.. – Maifee Ul Asad Feb 17 '20 at 15:44

2 Answers2

0

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.

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
0

Any ideas how to make it work with Asp.net core. I just test it wih my local machine ip address and work perfectly.

Unfortunately, not the case with Asp.net core.

aAndroid + Retrofit + Asp.net API "java.security.cert.CertPathValidatorException: Trust anchor for certification path not found."

Salim
  • 62
  • 1
  • 8
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. – Vyncent Sep 07 '21 at 09:12