【发布时间】:2018-11-20 16:26:08
【问题描述】:
我想调用 GitHub API 根据任意搜索参数返回存储库列表,如下所示:https://api.github.com/search/repositories?q=custom_search_param
所以 custom_search_param 是在运行时声明的。
我做了这个界面:
public interface GitHubClient {
String BASE_URL = "https://api.github.com/";
@GET("search/repositories")
Call<GitHubRepo> getReposForSearchParam (@Query("q") String custom_search_param);
}
我在 MainActivity onCreate 中这样称呼它:
Retrofit retrofit = new Retrofit.Builder().baseUrl(GitHubClient.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
GitHubClient gitHubClient = retrofit.create(GitHubClient.class);
Call<GitHubRepo> call = gitHubClient.getReposForSearchParam("tetris");
call.enqueue(new Callback<GitHubRepo>() {
@Override
public void onResponse(Call<GitHubRepo> call, Response<GitHubRepo> response) {
Log.d("resp",response.toString());
Log.d("respBody",response.body().toString());
}
@Override
public void onFailure(Call<GitHubRepo> call, Throwable t) {
Log.e("wentWrong", t.getMessage());
}
});
我总是收到onFailure 回复这条消息:
E/wentWrong:javax.net.ssl.SSLProtocolException:SSL 握手 中止:ssl=0xb8679dd0:SSL 库中的故障,通常是协议 错误 错误:1407742E:SSL 例程:SSL23_GET_SERVER_HELLO:tlsv1 警报 协议版本(外部/openssl/ssl/s23_clnt.c:741 0x9db10901:0x000
00000)
有谁知道这里出了什么问题以及是否应该以不同的方式声明 GitHubClient 接口注释?
【问题讨论】:
-
您是否错过了 api 请求的用户代理标头?见developer.github.com/v3/#user-agent-required
-
我已经在 getReposForSearchParam() 方法中添加了@Headers("User-Agent: mygithubusername") 并且它仍然是一样的
-
好的,你用的是什么设备,哪个api级别的设备?因为android 4.4以下的设备上有ssl issue。
-
我认为您收到此错误是因为 GitHub api disabled TLSv1.1 并且您的客户端似乎仍在尝试与 tlsv1 握手。
-
谢谢,你是对的,我用的是android 4.1模拟器,现在我在6.0上试过,我得到了正确的响应。