改型:预计为BEGIN_OBJECT,但为STRING [英] Retrofit: Expected BEGIN_OBJECT but was STRING

查看:67
本文介绍了改型:预计为BEGIN_OBJECT,但为STRING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的JSON文件 http://sawbo-illinois.org/mobileApp.php.我将对象创建为:

I have JSON file as this http://sawbo-illinois.org/mobileApp.php . I've created objects as:

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;
    }
}

但是像这样,我收到了Retrofit回调的失败响应.我的问题在哪里?

But I get a failure response from Retrofit call back as I followed like this. Where is my problem?

我的完整代码是:

改造界面:

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();

推荐答案

如果服务器响应为JSON文档,则您的代码将起作用.当前的响应是一个HTML文档,如果在Web浏览器中查看该响应,将会引起混乱(Web浏览器和登录OkHttpClient实例的内容相同)

Your code would work if the server response would be a JSON document. The response currently is an HTML document and that makes a confusion if viewing it an a web browser (the same content goes both for the web browser and logging in an OkHttpClient instance):

<!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>

您应该只修复mobileApp.php脚本,并删除所有与JSON结构无关的内容.如果将响应Content-Type标头设置为JSON MIME类型,那就太好了:

You should just fix the mobileApp.php script and remove all the content that is not related to JSON structure. It would be nice if the response Content-Type header would be set to a JSON MIME type: What is the correct JSON content type? .

这篇关于改型:预计为BEGIN_OBJECT,但为STRING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆