I am not able to use RestApi in android application


I am not able to use RestApi in android application
Here is my RestApi get call result
RestApi
An in my android application i am using the retrofit library to call this api to show items in my android application, here is the api call in android
package com.aditmsolutions.www.foodie;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface Api {
String BASE_URL = "https://10.0.2.2:51087/api/";
@GET("items")
Call<List<Item>> getItems();
}
When I run my app it gives this error message "HandShake failed"
error
Please help
And here is logcat view
And her is the code to getItems
private void getItems() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<List<Item>> call = api.getItems();
call.enqueue(new Callback<List<Item>>() {
@Override
public void onResponse(Call<List<Item>> call, Response<List<Item>> response) {
List<Item> itemList = response.body();
//Creating an String array for the ListView
String items = new String[itemList.size()];
//looping through all the heroes and inserting the names inside the string array
for (int i = 0; i < itemList.size(); i++) {
items[i] = itemList.get(i).getTitle();
}
//displaying the string array into listview
listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, items));
}
@Override
public void onFailure(Call<List<Item>> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
Also Post Item class and json response And Logcat error
– quick learner
17 mins ago
Json response is attached as picture
– Asim Javaid
15 mins ago
share the code where you are calling
getItems()
and also the retrofit call– Navneet Krishna
8 mins ago
getItems()
probably not related to retrofit. Does the base URL accept HTTPS connections? try with http first and see if it works
– Omar Al Halabi
3 mins ago
1 Answer
1
try this :
String BASE_URL = "https://10.0.2.2:51087/api/";
private void request(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api services = retrofit.create(Api.class);
Call<List<Item>> call = services. getItems();
Callback<List<Item>> callbacks = new Callback<List<Item>>() {
@Override
public void onResponse(Call<List< Item >> call, retrofit2.Response<List<Item>> response) {
if (response.isSuccessful()) {
List< Item > list = response.body();
// do ....
}
}
@Override
public void onFailure(Call<List<Item>> call, Throwable t) {
t.printStackTrace();
}
};
call.enqueue(callbacks);
}
API interface :
package com.aditmsolutions.www.foodie;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface Api {
@GET("items")
Call<List<Item>> getItems();
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
post your full error with stacktrace
– userI
18 mins ago