How to Fix Expected Begin_Object But Was String in Retrofit

Retrofit Expected BEGIN_OBJECT but was STRING

I found the issue. I forgot to set the Headers for the request. The ApiInterface must be look like this:

public interface ApiInterface {
@Headers("Accept: application/json")
@POST("api/auth/login")
Call<LoginResult> login(@Body Login login);
}

Retrofit 2.0: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

This usually happens when you're receiving something other than the expected response from the server.

To understand what's wrong try imitate your request in Postman and see what you receive from server.

Moreover you can turn on OkHttp's Logging Interceptor to see exactly what the server returns in your Logcat.

Retrofit: Expected BEGIN_OBJECT but was STRING

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



Related Topics



Leave a reply



Submit