gpt4 book ai didi

android - 使用 Retrofit 2 获取 SSLException

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:21:17 26 4
gpt4 key购买 nike

我正在创建授权应用程序,我正在使用 Retrofit 2。当我进行调用时,会转到 onFailure 方法并获取异常

“javax.net.ssl.SSLException:连接被对等方关闭”

但问题是,昨天这很管用。今天它给出了异常(exception)。我在网上找到一些SSLException - Connection closed by peer on Android 4.x versions ,或者这个 How to add TLS v 1.0 and TLS v.1.1 with Retrofit ,但这对我没有帮助。任何想法如何解决它。在后端启用 TLS1.2。

public class RegistrationFragment extends BaseFragment {
View mainView;

ApiClient apiClient = ApiClient.getInstance();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mainView = inflater.inflate
(R.layout.registration_fragment, container, false);

//Calling the authorization method
registerCall();
}
});

return mainView;
}

//User authorization method

public void registerCall() {

Call<ResponseBody> call = apiClient.registration(supportopObj);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {

//Calling the clientCall method for getting the user clientID and clientSecret
Toast.makeText(getActivity(), "Registration Successful ",
Toast.LENGTH_SHORT).show();;

} else {
//if the response not successful
Toast.makeText(getActivity(), "Could not register the user maybe already registered ",
Toast.LENGTH_SHORT).show();
}
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getActivity(), "An error occurred", Toast.LENGTH_SHORT).show();
}
});
}
}

最佳答案

不是因为Retrofit,而是因为okhttp。如果你使用 okhttp version 3.x ,你会遇到这个问题。直接的解决方案是使用 okhttp 版本 2.x。还有一点要补充的是,这个问题只发生在 Android 版本 16-20 Link for reference

关于android - 使用 Retrofit 2 获取 SSLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49358946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com