我在我的应用程序中使用 Retrofit 将数据从 JSON 读取到我的应用程序。我有这个 JSON。 (我尝试了很多解决方案,但对我没有任何帮助)。请帮助我。我使用了与其他 JSON 相同的代码代码,它适用于此 This JSON URL但它不适用于我的 JSON
JSON My JSON is placed at this URL
我的界面:
public interface SanaApi {
String BASE_URL = "http://www.hhfoodies.com/salesnotifier/";
@GET("sana_safinaz.php")
Call<List<SanaSafinas>> getSana();
}
这是模型类:
public class SanaSafinas {
private String id;
private String off;
private String image;
private String title;
private String sale_price;
private String original_price;
public SanaSafinas(String id, String off, String image, String title, String sale_price, String original_price) {
this.id = id;
this.off = off;
this.image = image;
this.title = title;
this.sale_price = sale_price;
this.original_price = original_price;
}
public String getId() {
return id;
}
public String getOff() {
return off;
}
public String getImage() {
return image;
}
public String getTitle() {
return title;
}
public String getSale_price() {
return sale_price;
}
public String getOriginal_price() {
return original_price;
}
}
这是主要 Activity :
public class MainActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listViewHeroes);
//calling the method to display the heroes
getSana();
}
private void getSana() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SanaApi.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
SanaApi api = retrofit.create(SanaApi.class);
Call<List<SanaSafinas>> call = api.getSana();
call.enqueue(new Callback<List<SanaSafinas>>() {
@Override
public void onResponse(Call<List<SanaSafinas>> call, Response<List<SanaSafinas>> response) {
List<SanaSafinas> heroList = response.body();
//Creating an String array for the ListView
String[] heroes = new String[heroList.size()];
//looping through all the heroes and inserting the names inside the string array
for (int i = 0; i < heroList.size(); i++) {
heroes[i] = heroList.get(i).getSale_price();
}
//displaying the string array into listview
listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, heroes));
}
@Override
public void onFailure(Call<List<SanaSafinas>> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
最佳答案
您的 retrofit 电话需要一个 List<SanaSafinas>
,它是从 JSON 数组生成的。
您的 JSON 不是一个数组,它是一个对象,这就是您需要告诉改造您期望的内容。
class Response {
private final List<SanaSafinas> data;
public Response(List<SanaSafinas> data) {
this.data = data;
}
// Getter...
}
然后您的界面将如下所示:
public interface SanaApi {
String BASE_URL = "http://www.hhfoodies.com/salesnotifier/";
@GET("sana_safinaz.php")
Call<Response> getSana();
JSON 对象像您一样用花括号括起来:
{
"message": "",
"status": 1,
"data": [
{
"id": "s18220052",
"off": "50% Off",
"image": "https://www.sanasafinaz.com/media/catalog/product/cache/2cf0e7f44e364cdf6f00ee795133d228/s/1/s18220052_1_.jpg",
"title": "SAFARI-O",
"sale_price": "PKR 2,720.00",
"original_price": "PKR 5440 "
},
...
}
JSON 数组包含在方括号中,就像您的第一个 url:
[
{
"name": "Captain America",
"realname": "Steve Rogers",
"team": "Avengers",
"firstappearance": "1941",
....
},
...
}
关于android - 应为 BEGIN_ARRAY 但为 BEGIN_OBJECT Android Retrofit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54284377/