2017-04-20 10 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; 
    } 
} 

私はこのようにして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(); 
+0

どのようにjsonを解析しようとしていますか?このようにconverter-gsonを使用してコード – AwaisMajeed

+0

を共有します: 'Retrofit retrofit = new Retrofit.Builder() .baseUrl(" http://sawbo-illinois.org ") .addConverterFactory(GsonConverterFactory.create()) .build ); ServiceAPIサービス= retrofit.create(ServiceAPI.class); final call

+0

これは?これはどこですか? – AwaisMajeed

答えて

0

を行ったようにあなたのコードは動作しますAPI呼び出しを行います。応答は、現在のHTML文書であり、それをWebブラウザ(同じ内容がOkHttpClientインスタンスで両方のWebブラウザとロギングのために行く)を見た場合にそれが混乱を作る:

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

私はあなたにスラッシュ(/)を追加する必要があると思いますあなたの最後のベースURLこのように:

このようなインターフェイスを使用してコールを作成します。

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

そして、サーバの応答がJSONドキュメントになる場合は、

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); 
} 
関連する問題