2017-04-20 107 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); 
} 
相關問題