0
我試圖通過改造從我的本地服務器獲取數據。我的電話是空的,並在logcat我的應用程序甚至不嘗試連接到我的服務器。但是,當我嘗試連接瀏覽器(在智能手機上)我得到我的JSON,所以我找不到問題。這裏是我的代碼改裝甚至不嘗試連接API
premissions
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
URLS
public static final String HOST = "http://192.168.0.103:8080/";
public static final String RESIPES = "recipes";
RetrofitClient
private static Retrofit getRetrofitInstance(){
return new Retrofit.Builder()
.baseUrl(Urls.HOST)
.addConverterFactory(JacksonConverterFactory.create())
.build();
}
public static ApiService getApiService(){
return getRetrofitInstance().create(ApiService.class);
}
ApiService
public interface ApiService {
@GET(Urls.RESIPES)
Call<List<RecipeDTO>> getData();
}
我不知道怎麼使用改裝
@Override
public void onClick(View view) {
//Проверка на соеденение с интернетом
if(InternetConnection.checkConnection(context)){
final ProgressDialog dialog;
//Диалог с пользователем
dialog = new ProgressDialog(context);
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.setTitle(getString(R.string.gettin_Gson_dialog_title));
dialog.setMessage(getString(R.string.gettin_Gson_dialog_message));
dialog.show();
//Создаем объект нашего апи
ApiService api = RetrofitClient.getApiService();
//Вызываем джесон
Call<List<RecipeDTO>> call = api.getData();
// кол бэк будет вызван как только получит ответ
call.enqueue(new Callback<List<RecipeDTO>>() {
@Override
public void onResponse(Call<List<RecipeDTO>> call, Response<List<RecipeDTO>> response) {
//Dismiss Dialog
dialog.dismiss();
if (response.isSuccessful()) {
recipeList.addAll(response.body());
adapterRecipes = new RecipesAdapter(context, recipeList);
recyclerView.setAdapter(adapterRecipes);
adapterRecipes.notifyDataSetChanged();
} else {
Toast toast = Toast.makeText(context, R.string.something_wrong, Toast.LENGTH_SHORT);
toast.show();
}
}
@Override
public void onFailure(Call<List<RecipeDTO>> call, Throwable t) {
Toast toast = Toast.makeText(context, R.string.something_wrong, Toast.LENGTH_SHORT);
toast.show();
}
});
}
}});
型號
公共類RecipeDTO {
@SerializedName("idRecipe")
@Expose
private long idRecipe;
@SerializedName("nameRecipe")
@Expose
private String nameRecipe;
@SerializedName("category")
@Expose
private CategoryDTO category;
@SerializedName("descriptionRecipe")
@Expose
private String descriptionRecipe;
@SerializedName("tutorialRecipe")
@Expose
private String tutorialRecipe;
@SerializedName("image")
@Expose
private byte[] image;
@SerializedName("ingridientRecipe")
@Expose
private List<IngredientDTO> ingridientRecipe;
public RecipeDTO(){
}
//getters setters
這是github上的這個項目,如果我錯過了smth。
https://github.com/xdewnik/StackQuestion分支改造實現
,並在這裏如果有人需要https://github.com/xdewnik/CookingServer
其中是RecipeDTO的數據模型類? –
@AalapPatel added –
所以,如果您在改造調用時沒有收到任何錯誤,並且使用response.isSuccessful方法成功響應,則需要檢查您的響應的JSON字段以及您給出的字段的模型類引用。 –