- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的应用程序中使用 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/
我在改造方面遇到问题。 CryptoCompareClient.java public interface CryptoCompareClient { @GET("/data/pricemulti")
我正在尝试将 errorBody 转换为我的 RegistrationResponse 但它总是抛出一个 Expected BEGIN_ARRAY but was BEGIN_OBJECT 注册响应
我是初学者,我正在我的应用中使用 Retrofit 2。我有这个 JSON。 (我尝试了很多解决方案,但对我没有任何帮助)。感谢您的帮助 我遇到的错误:应为 BEGIN_ARRAY 但在第 1 行第
我是java初学者,我需要一些帮助,我看过很多关于的帖子,但没有解决他们的问题 我有来自 url 的 json 对象,就像这样 { 'history':[{ 'id':
我正在使用 Retrofit 2 发送 HTTP 请求,我已经定期完成了所有操作,但是,情况“不标准”。我有一个问题,响应类中要接收的对象之一曾经作为对象发送,而另一个作为列表发送。所以我不知道是在响
我从 .NET WS 获取 JSON 响应(项目列表),因此我想在 Android 应用程序中转换为列表。但是当使用 GSON 库时,出现以下异常: com.google.gson.JsonSynta
如何使用 Retrofit 解析此内容?我收到错误 BEGIN_OBJECT but was BEGIN_ARRAY Json 如下,它包含一个结果数组对象,该对象有一个为 null 的数组对象和信息
我正在努力通过改造和 RXAndroid 从 Android 与我的 REST API 进行交互,但遇到了问题。 当我向“api/users/{id}/{id}/classes”发送 GET 请求时,
我在使用 retrofit 调用进行解析时遇到问题。这不是重复的问题。我尝试了太多的谷歌搜索也尝试了很多解决方案,但它在我的情况下不起作用。所以请不要对这个问题投反对票。 错误 05-04 04:18
我今天才开始使用 Retrofit 库。我在获取一些 json 数据时遇到问题。 我尝试获取的 JSON 数据如下所示: [ {"id":"1","genre_id":"27","name":"
我正在尝试解析 the JSON response from this link我得到了这个异常(exception): Expected BEGIN_ARRAY but was BEGIN_OBJE
我正在努力学习 MVP。我在教程和 YouTube 视频的帮助下编写了代码。首先,我使用了 Json2Class 插件。之后,我创建了一个 Json-Server,只是为了从 REST API 获取一
在api响应中有时可以是数组,有时可以是字符串。 这里的细节是Array { "ts": "2015-06-16 11:28:33","success": true,"error": false,
您好,我是 Retrofit 库的新手,我在解析某些 json 时遇到问题。我已经查看了 Stackoverflow 上的其他一些解决方案,但对我的问题不太满意。我试图让一个简单的网络服务工作。任何建
这是我的 JSON 数据: [ { "page": 0, "threads": [ { "
我对 JSON 解析相当陌生,我正在使用 Square 的 Retrofit 库并遇到了这个问题。 我正在尝试解析这个 JSON 响应: [ { "id": 3,
我从服务中得到一个 json 结果,如下所示: { "result": { "totalCount": 15, "resultCount": 2,
我在 gson 反序列化方面遇到问题。传入的 json 看起来像这样 {"roles":{"name":"bla""perm": "bla"}} 或者当有更多可用角色时,它看起来像这样 {"roles
首先,我知道其他一些用户之前已经问过类似的问题,所以您可能认为这是一个重复的问题,但事实并非如此。 我想创建一个模型类来解析 GoogleMaps-Direction API 响应从 JSON 格式到
我目前正在使用 Retrofit 来调用此 URL 。我正在努力理解如何基于 JSON 映射 POJO。这是我的代码: final ApiInterface apiInterface = retr
我是一名优秀的程序员,十分优秀!