当前位置: 首页 > 知识库问答 >
问题:

改装:应为BEGIN\u对象,但为字符串

昝涛
2023-03-14

我有如下JSON文件http://sawbo-illinois.org/mobileApp.php . 我已将对象创建为:

public class Video {
    public List<all> all;
    public List<String>Language;
    public List<String>Country;
    public List<String>Topic;
    public class all{
        public String id;
        public String Country;
        public String Language;
        public String Topic;
        public String Title;
        public String Video;
        public String Videolight;
        public String Description;
        public String Image;
    }
}

但是当我像这样跟着的时候,我得到了改造失败的回应。我的问题在哪里?

我的完整代码是:

改造界面:

public interface ServiceAPI {

    @GET("mobileApp.php")
    Call<Video> getVideoDetails();
}

可能回调和转换过程:

 Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://sawbo-illinois.org/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

 ServiceAPI service = retrofit.create(ServiceAPI.class);

 final Call<Video> call = service.getVideoDetails();

共有3个答案

易和怡
2023-03-14
if (response.code() == 200) {        
    String result = response.body().string();
    Gson gson = new Gson();
    pojo = gson.fromJson(result, PojoClass.class);
}
高朝明
2023-03-14

我认为您需要在baseUrl的末尾添加斜杠(/),如下所示:

使用如下界面创建调用:

public interface ApiWebServices {
    @GET()
    Call<Video> getVideoDetails(@Url String url);
}

然后像上面一样进行API调用

Call<Video> call = service.getVideoDetails("http://sawbo-illinois.org/mobileApp.php");
call.enque(..);
司空修贤
2023-03-14

如果服务器响应是JSON文档,那么代码就可以工作。当前的响应是一个HTML文档,如果在web浏览器中查看它,会造成混淆(web浏览器和登录到OkHttpClient实例的内容相同):

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Testing Connection Page</title>
</head>

<body>
{"all":[{"id":"0","Country":"Brazil","Language":"Portuguese","Topic":"HandWashing","Title":"How to Wash Your Hands","Video":"AKA1_Fante_Ghana_HandWashing_Final.3gp","Videolight":"AKA1_Fante_Ghana_HandWashing_Final_Light.3gp","Description":"Washing hands is the best way to prevent the spread of germs and diseases. Dirty hands can carry pathogenic germs that can sicken a person or spread diseases to others. Microorganisms such as bacteria, viruses, parasites, fungi and various chemicals can enter our bodies directly when we touch our face, eyes, nose or mouth or may enter indirectly, when our dirty hands stain surfaces touched by others or where food is prepared. The habit of washing hands with soap and water constitutes the first line of defense against the spread of many diseases, from the common cold or diarrhea to more serious illnesses such as meningitis, influenza or hepatitis as well as many other diseases. This 2-D animation describes the importance of hand washing.","Image":"HandWashing.jpg"},{"id":"1","Country":"Kenya","Language":"Swahili","Topic":"SGComposting3D","Title":"Survival Gardening: How to Create Compost (3D)","Video":"SW_Swahili_Kenya_SGComposting3D_Final.3gp","Videolight":"SW_Swahili_Kenya_SGComposting3D_Final_Light.3gp","Description":"Compost can be used to improve the quality of your soil. You can use plant materials, animal manure and kitchen scraps to create compost. Compost will add nutrients and organic matter to your soil. This animation explains the process of creating and storing compost.","Image":"SGComposting3D.jpg"}],"Language":["Portuguese","Swahili"],"Topic":["HandWashing","SGComposting3D"],"Country":["Brazil","Kenya"]}
</body>
</html>

您应该只修复mobile eApp.php脚本并删除所有与JSON结构无关的内容。如果响应Content-Type标头设置为JSON MIME类型会很好:什么是正确的JSON内容类型?。

 类似资料:
  • 我从以下改装服务中获得了一个json结果: 我在改装后变得像bellow一样: 但是告诉我: 下面是我的模型:

  • 我正在尝试使用《纽约时报》API,并使用Observable进行改装。但我在尝试使用数据时遇到了这个错误。 有人能帮我看看哪里错了吗? 以下是我的ApiServices界面: 这是我的ApiStreams课 这就是我在我的主要活动中要做的。现在我只想在文本视图中显示每个标题的列表。。。 [编辑]TopStories和NewsItem有我的两个模型 上部结构: 新闻项目: 以下是JSON的外观: J

  • 这是我的JSON数据: 这是我的反序列化代码: 我在PageSecurity上收到错误。这是我的PageResponce类。我想我可能处理信息是正确的。我只使用过JsonObject。我想知道如何获取“page”: 0,然后如何获取“线程” } 我的页面类 公共类页面{ }

  • 我有下面的JSON和模型类,如何使用GSOn反序列化这个JSON,下面的代码片段会出现类似“预期的BEGIN\u对象,但是BEGIN\u数组”的异常。如何为下面的JSON数据生成模型CALS

  • 我有这样的json。在我的情况下,当我单击一个产品时,我会将该产品交给detailproduct。在那之后,我在我的详细信息屏幕中有了回收器。我想按产品数组获取回收商项目,该数组的id等于单击的产品id。 我想了解产品阵列。我的api方法正在返回调用

  • 我试图只获取字符串请求,但它的给定错误如下 我的API输出如下: 响应代码: