I am new to Android and I want to make an application based on REST API.
So I search and found the Retrofit 2.
I used a sample of retrofit that I found in Github to learn how it works.
This files are my Java classes :
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;
}
}
JSON code from Github
{
...
"name": "Google",
"blog": "https://opensource.google.com/",
...
}
I want to get the name and blog URL
And when I run the program the output is:
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)
What's the problem and how can I fix?
The first step here would be to understand the error.
Try printing the error from your onFailure method like this:
@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();
}
You should get in your logcat some more useful information about what went wrong, it will be easier to figure out how to solve it after that Also have you added internet access permission in your manifest?
SSL Exception comes when SSL Certificate issue . You need to add ssl certificate to server side.
Have you tried
android:networkSecurityConfig="@xml/network_security_config"
in your manifest tag
also put the following file in 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>
User contributions licensed under CC BY-SA 3.0