【问题标题】:Why my get String http in retrofit don't work?为什么我在改造中的 get String http 不起作用?
【发布时间】:2018-01-30 18:49:22
【问题描述】:

我有一个链接和一个 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;
    }
}

我打电话:

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();
        }
    });

但是不行,进不了函数onResponse

【问题讨论】:

  • 它会转到onFailure 吗?!您可以在 onFailure 中打印 t.getMessage() 并将其添加到您的问题中,以了解更多关于发生了什么的线索吗?
  • 在检查 if 条件之前,只需放一个日志,看看你得到什么响应
  • 当我使用链接时:gateway.fpts.com.vn/g5g/fpts/?s=realtime_index_ho ---------------- baseUrl: gateway.fpts .com.vn/g5g/fpts ---------- @GET("") Call getStringResponse(@Query("s") String姓名);它不起作用!

标签: android retrofit2


【解决方案1】:
@GET("users/{username}")

如果您的基本 url 是 https://www.youtube.com/watch?v=,那么通过改造为方法 getStringResponse() 创建的 url 是 https://www.youtube.com/watch?v=users/EMyW7WvhEso

您的基本网址应为 https://www.youtube.com/ 并定义您的 api,如下所示:

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

【讨论】:

猜你喜欢
  • 2013-05-24
  • 1970-01-01
  • 2020-11-15
  • 2015-03-06
  • 2023-01-22
  • 2020-05-11
  • 2023-01-01
  • 2020-09-22
  • 2021-05-20
相关资源
最近更新 更多