Chief Madog
Chief Madog

Reputation: 1507

Retrofit Can't return Data

i'm probebly doing something wrong, i'm trying to figure out how to use retrofit, so for now i'm calling back just a general ResponseBody, and not yet parsing anything, (just a simple http get)

but retrofit can't get the data, what am i doing wrong ? >

my Retrofit API >

public interface retrofitApi {

    String baseUrl = "http://localhost:3003/";

    @GET("api/radBox/getDegrees")
    Call<ResponseBody> getCallData();


    class Factory {
        private static retrofitApi service;

        public static retrofitApi getInstance() {
            if (service == null) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(baseUrl)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

                service = retrofit.create(retrofitApi.class);
                return service;
            } else {
                return service;
            }

        }
    }


    }

and in my main Activity i put >

retrofitApi.Factory.getInstance().getCallData().enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

            Log.d("myLogs", "log: " + response);

        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.d("myLogs", "failed to Retrive Data");


}
        });

Upvotes: 1

Views: 693

Answers (2)

xiaoyaoworm
xiaoyaoworm

Reputation: 1102

I think your problem is caused by using "localhost". Looks like you are using your phone to connect to phone's 3003 port. Exchange the localhost to your Server IP to give a try.

I copy all your code in my retrofit project, I exchange the URL, everything is working well on my side, meaning your retrofit code has no problem.

Upvotes: 1

Yury Pashkov
Yury Pashkov

Reputation: 241

Try use instead ResponseBody your model (for me it WeatherModel) which must will return from server. Like this

String baseUrl = "http://api.openweathermap.org/";

@GET("data/2.5/weather")
Call<WeatherModel> getCallData(@Query("q") String q, @Query("lang") String lang,
                               @Query("appid") String appid);

and put in MainActivity like this

retrofitApi.Factory.getInstance()
            .getCallData("Taganrog,ru", "ru", "bde41abf61e82b6209a544a5ea2ddb76")
            .enqueue(new Callback<WeatherModel>() {
        @Override
        public void onResponse(Call<WeatherModel> call, Response<WeatherModel> response) {
            Log.d("myLogs", "log: " + response);
        }

        @Override
        public void onFailure(Call<WeatherModel> call, Throwable t) {
            Log.d("myLogs", "log: " + "failed to Retrive Data");
        }
    });

It's works fine for me

Upvotes: 0

Related Questions