Trang Đỗ
Trang Đỗ

Reputation: 180

Why my get String http in retrofit don't work?

I have a link and an API:

@GET("users/{username}")
Call<String> getStringResponse(@Path("username") String username);

public class APIClientDetail {

public Retrofit getRetrofit(String baseUrl) {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(ScalarsConverterFactory.create())
            // add other factories here, if needed.
            .build();

        return retrofit;
    }
}

I call:

APIClientDetail apiClientDetail= new APIClientDetail();
    Retrofit retrofit= apiClientDetail.getRetrofit("https://www.youtube.com/watch?v=");

    GitHubService scalarService = retrofit.create(GitHubService.class);
    Call<String> stringCall = scalarService.getStringResponse("EMyW7WvhEso");
    stringCall.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            if (response.isSuccessful()) {
                String responseString = response.body();
                // todo: do something with the response string
                modelInterface.dataSuccessful();
            }
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            modelInterface.dataError();
        }
    });

But it don't work, not into the function onResponse.

Upvotes: 0

Views: 339

Answers (1)

denvercoder9
denvercoder9

Reputation: 3021

@GET("users/{username}")

If your base url is https://www.youtube.com/watch?v=, then the url being created by retrofit for the method getStringResponse() is https://www.youtube.com/watch?v=users/EMyW7WvhEso.

Your base url should be https://www.youtube.com/ and define your api like the following:

@GET("watch")
Call<String> getVideo(@Query("v") String videoID);

Upvotes: 1

Related Questions