首页 > 解决方案 > 连接到 API 时改造应用程序崩溃

问题描述

所以我正在制作一个连接到具有以下数据的配方 api 的应用程序:

{"count": 30, "recipes": 
[{"publisher": "Simply Recipes", "f2f_url": "http://food2fork.com/view/36251", "title": "Easy Brazilian Cheese Bread", "source_url": "http://www.simplyrecipes.com/recipes/easy_brazilian_cheese_bread/", "recipe_id": "36251", "image_url": "http://static.food2fork.com/braziliancheesebreada300x200ffd79a7b.jpg", "social_rank": 100.0, "publisher_url": "http://simplyrecipes.com"}, 
{"publisher": "All Recipes", "f2f_url": "http://food2fork.com/view/32745", "title": "To Die For Blueberry Muffins", "source_url": "http://allrecipes.com/Recipe/To-Die-For-Blueberry-Muffins/Detail.aspx", "recipe_id": "32745", "image_url": "http://static.food2fork.com/6629086e7e.jpg", "social_rank": 100.0, "publisher_url": "http://allrecipes.com"}, 
{"publisher": "All Recipes", "f2f_url": "http://food2fork.com/view/3068", "title": "Basic Crepes", "source_url": "http://allrecipes.com/Recipe/Basic-Crepes/Detail.aspx", "recipe_id": "3068", "image_url": "http://static.food2fork.com/4005704623.jpg", "social_rank": 100.0, "publisher_url": "http://allrecipes.com"}, 
{"publisher": "The Pioneer Woman", "f2f_url": "http://food2fork.com/view/fbc7af", "title": "The Best Chocolate Sheet Cake. Ever.", "source_url": "http://thepioneerwoman.com/cooking/2007/06/the_best_chocol/", "recipe_id": "fbc7af", "image_url": "http://static.food2fork.com/388604527_5e6812454fc6f7.jpg", "social_rank": 100.0, "publisher_url": "http://thepioneerwoman.com"}, 
{"publisher": "All Recipes", "f2f_url": "http://food2fork.com/view/33124", "title": "Tres Leches Cake", "source_url": "http://allrecipes.com/Recipe/Tres-Leches-Milk-Cake/Detail.aspx", "recipe_id": "33124", "image_url": "http://static.food2fork.com/384988ad12.jpg", "social_rank": 99.99999999999999, "publisher_url": "http://allrecipes.com"}, 
{"publisher": "All Recipes", "f2f_url": "http://food2fork.com/view/10467", "title": "Creamy Rice Pudding", "source_url": "http://allrecipes.com/Recipe/Creamy-Rice-Pudding/Detail.aspx", "recipe_id": "10467", "image_url": "http://static.food2fork.com/1086813697.jpg", "social_rank": 99.99999999999999, "publisher_url": "http://allrecipes.com"}, 
{"publisher": "Closet Cooking", "f2f_url": "http://food2fork.com/view/35499", "title": "Pumpkin Pie French Toast", "source_url": "http://www.closetcooking.com/2009/11/pumpkin-pie-french-toast.html", "recipe_id": "35499", "image_url": "http://static.food2fork.com/PumpkinPieFrenchToast15002845c7fe.jpg", "social_rank": 99.99999999999997, "publisher_url": "http://closetcooking.com"}, 
{"publisher": "Two Peas and Their Pod", "f2f_url": "http://food2fork.com/view/54363", "title": "Chocolate Sheet Cake with Peanut Butter Frosting", "source_url": "http://www.twopeasandtheirpod.com/chocolate-sheet-cake-with-peanut-butter-frosting/", "recipe_id": "54363", "image_url": "http://static.food2fork.com/chocolatesheetcakewithpeanutbutterfrosting3a2ac.jpg", "social_rank": 99.99999999999997, "publisher_url": "http://www.twopeasandtheirpod.com"}]

以下是模型:

package com.example.mobilniproekt.model;

import com.google.gson.annotations.SerializedName;

public class Recipe {

@SerializedName("publisher")
private String publisher;

@SerializedName("f2f_url")
private String f2f_url;

@SerializedName("title")
private String title;

@SerializedName("source_url")
private String source_url;

@SerializedName("recipe_id")
private String recipe_id;

@SerializedName("image_url")
private String image_url;

@SerializedName("social_rank")
private Float social_rank;

@SerializedName("publisher_url")
private String publisher_url;

public Recipe(String publisher, String f2f_url, String title, String source_url, String recipe_id, String image_url, Float social_rank, String publisher_url) {
    this.publisher = publisher;
    this.f2f_url = f2f_url;
    this.title = title;
    this.source_url = source_url;
    this.recipe_id = recipe_id;
    this.image_url = image_url;
    this.social_rank = social_rank;
    this.publisher_url = publisher_url;
}

public String getPublisher() {
    return publisher;
}

public String getF2f_url() {
    return f2f_url;
}

public String getTitle() {
    return title;
}

public String getSource_url() {
    return source_url;
}

public String getRecipe_id() {
    return recipe_id;
}

public String getImage_url() {
    return image_url;
}

public Float getSocial_rank() {
    return social_rank;
}

public String getPublisher_url() {
    return publisher_url;
}

public void setPublisher(String publisher) {
    this.publisher = publisher;
}

public void setF2f_url(String f2f_url) {
    this.f2f_url = f2f_url;
}

public void setTitle(String title) {
    this.title = title;
}

public void setSource_url(String source_url) {
    this.source_url = source_url;
}

public void setRecipe_id(String recipe_id) {
    this.recipe_id = recipe_id;
}

public void setImage_url(String image_url) {
    this.image_url = image_url;
}

public void setSocial_rank(Float social_rank) {
    this.social_rank = social_rank;
}

public void setPublisher_url(String publisher_url) {
    this.publisher_url = publisher_url;
}
}


package com.example.mobilniproekt.model;

import com.google.gson.annotations.SerializedName;

import java.util.List;

第二种型号:

public class Recipes {

@SerializedName("count")
private Integer count;

@SerializedName("recipes")
private List<Recipe> recipeList;

public Recipes(Integer count, List<Recipe> recipeList) {
    this.count = count;
    this.recipeList = recipeList;
}

public Integer getCount() {
    return count;
}

public List<Recipe> getRecipeList() {
    return recipeList;
}

public void setCount(Integer count) {
    this.count = count;
}

public void setRecipeList(List<Recipe> recipeList) {
    this.recipeList = recipeList;
}
}

改造客户:

package com.example.mobilniproekt.retrofit;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClientInstance {

private static Retrofit retrofit;
private static final String BASE_URL = "https://www.food2fork.com";

public static Retrofit getRetrofitInstance() {
    if (retrofit == null) {
        retrofit = new retrofit2.Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}


}

和界面:

package com.example.mobilniproekt.retrofit;

import com.example.mobilniproekt.model.Recipes;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

public interface GetDataService {

@GET("/api/search?key=API_KEY&q=milk,eggs")
Call<Recipes> getRecipes();

}

最后是主要实现:

public class ListActivity extends AppCompatActivity {

private CustomAdapter adapter;
private RecyclerView recyclerView;
ProgressDialog progressDoalog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    progressDoalog = new ProgressDialog(this);
    progressDoalog.setMessage("Loading....");
    progressDoalog.show();

    /*Create handle for the RetrofitInstance interface*/
    GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);
    Call<Recipes> call = service.getRecipes();
    call.enqueue(new Callback<Recipes>() {
        @Override
        public void onResponse(Call<Recipes> call, Response<Recipes> response) {
            progressDoalog.dismiss();
            TextView textViewTest=(TextView) findViewById(R.id.textViewTest);
            textViewTest.setText(response.body().getRecipeList().get(10).getTitle());
            Log.d(response.body().getRecipeList().get(10).getTitle(), response.body().getRecipeList().get(10).getPublisher());
            //generateDataList(response.body().getRecipeList());
        }

        @Override
        public void onFailure(Call<Recipes> call, Throwable t) {
            progressDoalog.dismiss();
        }
    });
}
}

但是,当我在手机上运行该应用程序时,该应用程序立即崩溃。当我声明 Float social_rank 并将其更改为 double 时,我认为错误出在模型中,但这并没有帮助,所以我切换回 float。

有任何想法吗?

标签: javaandroidretrofit

解决方案


推荐阅读