改造:预计BEGIN_OBJECT但是STRING

时间:2017-04-20 05:17:27

标签: android gson retrofit

我有http://sawbo-illinois.org/mobileApp.php的JSON文件。我创建了对象:

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回复的失败回应,因为我这样跟着。我的问题在哪里?

我的完整代码是:

Retrofit界面:

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 个答案:

答案 0 :(得分:1)

我认为您需要在 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(..);

答案 1 :(得分:0)

如果服务器响应是JSON文档,您的代码将起作用。目前的响应是一个HTML文档,如果将其视为Web浏览器会产生混淆(Web浏览器和登录Glide.with (context).load (url).asBitmap().into(imageView); 实例的内容相同):

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> 脚本并删除与JSON结构无关的所有内容。如果将响应mobileApp.php标头设置为JSON MIME类型:What is the correct JSON content type?,那就太好了。

答案 2 :(得分:0)

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