2017-04-20 59 views
0

我有这样的JSON文件http://sawbo-illinois.org/mobileApp.php。我创建的对象为:改装:预计BEGIN_OBJECT,但是STRING

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

你是如何解析json?共享代码 – AwaisMajeed

+0

与转换器-GSON作为这样的:'改造改型=新Retrofit.Builder() .baseUrl( “http://sawbo-illinois.org”) .addConverterFactory(GsonConverterFactory.create()) .build( ); ServiceAPI服务= retrofit.create(ServiceAPI.class); final致电

+0

as this ????这是哪里? – AwaisMajeed

回答

0

您的代码会工作的API调用,如果服务器响应将是一个JSON文件。响应目前是一个HTML文件,使混乱,如果查看它的网络浏览器(同样的内容是双向的网络浏览器,并记录在一个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> 

您应该只是修复mobileApp.php脚本并删除所有与JSON结构无关的内容。如果响应Content-Type标题将被设置为JSON MIME类型:What is the correct JSON content type?将会很好。

+0

谢谢你的帮助。 – Mahsa

0

我认为你需要添加斜杠(/)baseUrl这样结束:

这样创建界面您的来电:

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

然后让就像你上面

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

我分享了完整的代码,我按照你的说法进行了操作,但在这种情况下没有查询。 – Mahsa

+0

@Mahsa当没有端点时,您可以在您的Retrofit调用参数中传递整个url。 –

+0

感谢您的回应,我已按照您的指南进行操作,但仍得到相同的错误“期望的BEGIN_OBJECT,但在第3行第1列路径处为STRING”。服务器有可能发生错误吗?我不熟悉服务器开发。 – Mahsa

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