1

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();
Lyubomyr Shaydariv
  • 18,039
  • 11
  • 53
  • 97
Mahsa
  • 67
  • 1
  • 8
  • how are you trying to parse json??? share code – AwaisMajeed Apr 20 '17 at 05:22
  • with converter-gson as this: `Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://sawbo-illinois.org") .addConverterFactory(GsonConverterFactory.create()) .build(); ServiceAPI service = retrofit.create(ServiceAPI.class); final Call – Mahsa Apr 20 '17 at 05:23
  • as this???? where's this??? – AwaisMajeed Apr 20 '17 at 05:24
  • Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://sawbo-illinois.org") .addConverterFactory(GsonConverterFactory.create()) .build(); ServiceAPI service = retrofit.create(ServiceAPI.class); final Call – Mahsa Apr 20 '17 at 05:25
  • I can't see you requesting `mobileApp.php` path, only `http://sawbo-illinois.org` url. – Vladyslav Matviienko Apr 20 '17 at 05:35
  • Can you share the retrofit interface too – Aldrin Mathew Apr 20 '17 at 05:48
  • `public interface ServiceAPI { @GET("mobileApp.php") Call – Mahsa Apr 20 '17 at 05:50
  • I suspect the response return from server is not GSON as you specified. I worked with retrofit2 with okhttp on Android. Best way to debug is to enable logging. This article shows how to enable logging with `HttpLoggingInterceptor` : https://futurestud.io/tutorials/retrofit-2-log-requests-and-responses – Shuwn Yuan Tee Apr 20 '17 at 06:26

3 Answers3

1

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(..);
Nir Patel
  • 339
  • 3
  • 15
  • I share complete code, I've followed as you said, but there is no query in this case. – Mahsa Apr 20 '17 at 05:55
  • @Mahsa you can pass the whole url in your Retrofit call argument when there is no endpoint. – Nir Patel Apr 20 '17 at 06:05
  • thanks for your responce, I've followed your guide, but still get the same error "Expected BEGIN_OBJECT but was STRING at line 3 column 1 path". is it possible that error happens in server? I'm not familiar with server developing. – Mahsa Apr 20 '17 at 06:21
  • @Mahsa Your response is fine. Try creating your Response class using : http://www.jsonschema2pojo.org/ Source Type : json Annotation style : gson. It will provide you two classes as per your response. use them in your call and then check. – Nir Patel Apr 20 '17 at 06:29
1

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

Community
  • 1
  • 1
Lyubomyr Shaydariv
  • 18,039
  • 11
  • 53
  • 97
0
if (response.code() == 200) {        
    String result = response.body().string();
    Gson gson = new Gson();
    pojo = gson.fromJson(result, PojoClass.class);
}
Kalamarico
  • 4,668
  • 22
  • 44
  • 67
Krishna
  • 1,028
  • 11
  • 17