简体   繁体   中英

Retrofit: Expected BEGIN_OBJECT but was STRING

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

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

My complete code is:

The Retrofit interface:

public interface ServiceAPI {

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

May callback and convert process:

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

I think you need to add slash (/) at the end in your baseUrl like this :

Create your call with interface like this :

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

Then make the API call just like you did above

Call<Video> call = service.getVideoDetails("http://sawbo-illinois.org/mobileApp.php");
call.enque(..);

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>

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? .

if (response.code() == 200) {        
    String result = response.body().string();
    Gson gson = new Gson();
    pojo = gson.fromJson(result, PojoClass.class);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM