使用Retrofit 2的未知问题

时间:2018-09-21 19:18:25

标签: java android retrofit2

我是Android的新手,我想创建一个基于REST API的应用程序。

因此我搜索并找到了翻新2。

我使用在Github中找到的改造样本来了解其工作原理。

这些文件是我的Java类:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textv = findViewById(R.id.textv);
        GitHubClient gitHubClient;
        gitHubClient = ServiceGenerator.createService(GitHubClient.class);

        final Call<GitHubUser> call = gitHubClient.getFeed("google");
        call.enqueue(new Callback<GitHubUser>() {
            @Override
            public void onResponse(Call<GitHubUser> call, Response<GitHubUser> response) {
                GitHubUser gitModel = response.body();
                if (gitModel != null) {
                    textv.setText(getString(R.string.main_response_text,
                            gitModel.getName(),
                            gitModel.getBlog()));
                } else {
                    textv.setText("user doesn't exist");
                }
            }

            @Override
            public void onFailure(Call<GitHubUser> call, Throwable t) {
                textv.setText(t.getMessage());
            }

        });

    }

}

GithubClient.java

public interface GitHubClient {
    @GET("users/{user}")
    Call<GitHubUser> getFeed(@Path("user") String user);
}

ServiceGenerator.java

public class ServiceGenerator {

    public static final String API_BASE_URL = "https://api.github.com/";

    private static HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    private static Interceptor logging = interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);

    private static OkHttpClient httpClient = new OkHttpClient.Builder()
            .addInterceptor(logging)
            .build();

    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl(API_BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create());

    public static <S> S createService(Class<S> serviceClass) {
        Retrofit retrofit = builder.client(httpClient).build();
        return retrofit.create(serviceClass);
    }
}

GithubUser.java

public class GitHubUser {

    private String name;
    private String blog;

    public String getName() {
        return name;
    }

    public String getBlog() {
        return blog;
    }

}

来自Github的JSON代码

{
  ...
  "name": "Google",
  "blog": "https://opensource.google.com/",
  ...
}

我想获取名称和博客URL

当我运行程序时,输出为:

javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb8952b10: Failure in SSL library, usually a protocol error
error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version(external/openssl/ssl/s23_clnt.c:741 0x83bc3770:0x00000000)

有什么问题,我该如何解决?

3 个答案:

答案 0 :(得分:0)

这里的第一步是了解错误。

尝试像这样通过onFailure方法打印错误:

@Override
public void onFailure(Call<GitHubUser> call, Throwable t) {
    textv.setText("unknown error"); // can also do textv.setText(t.getMessage()) to display error reason
    t.printStackTrace();
}

您应该在logcat中获取有关发生问题的更多有用信息,在此之后更容易找出解决方法 您还在清单中添加了Internet访问权限吗?

答案 1 :(得分:0)

当SSL证书颁发时,SSL异常到来。您需要将ssl证书添加到服务器端。

答案 2 :(得分:0)

您尝试过

android:networkSecurityConfig="@xml/network_security_config"

在清单标签中

还将以下文件放入res >> xml >>

network_security_config.xml

<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">static.food2fork.com</domain>
    </domain-config>
</network-security-config>