This is the error I'm getting:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
JSON response I'm getting from Postman:
{
"title": "test title",
"body": "test body",
"userId": 1,
"id": 101
}
This is how I echoed the response from the rest API (used slim2 framework):
$app->post('/posts', function() use($app){
$res = array(
"title" => $app->request->post('title'),
"body" => $app->request->post('body'),
"userId" => 1,
"id" => 101
);
echoResponse(201, $res);
});
echoResponse
method:
function echoResponse($status_code, $response) {
$app = \Slim\Slim::getInstance();
$app->status($status_code);
$app->contentType('application/json');
echo json_encode($response);
}
The method where I call the API:
public void sendPost(String title, String body) {
mAPIService.savePost(title, body, 1).enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
Log.i(TAG, "post sendPost to onResponse.");
if(response.isSuccessful()) {
showResponse(response.body().toString());
Log.i(TAG, "post submitted to API." + response.body().toString());
}
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
Log.e(TAG, "Unable to submit post to API.");
Log.e(TAG, t.toString());
}
});
}
The APIServie
interface:
public interface APIService {
@POST("/posts")
@FormUrlEncoded
Call<Post> savePost(@Field("title") String title,
@Field("body") String body,
@Field("userId") long userId);
}
How I got the retrofit
instance:
mAPIService = ApiUtils.getAPIService();
public class ApiUtils {
private ApiUtils() {}
public static final String BASE_URL = "http://myurl/v1/";
public static APIService getAPIService() {
return RetrofitClient.getClient(BASE_URL).create(APIService.class);
}
}
public class RetrofitClient {
private static Retrofit retrofit = null;
private static Gson gson = new GsonBuilder()
.setLenient()
.create();
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
Reason I posted all this was I couldn't find where the problem is because JSON response I'm getting from API looks ok, the way I called the API looks ok. There are many questions related to this can be found here. But I couldn't find the answer to my problem. Please find me a solution to this. Thanks
EDIT : @Darpan and @Fred was right. I enabled the logging for retrofit. this is what it says
D/OkHttp: --> POST http://myurl/posts http/1.1
D/OkHttp: Content-Type: application/x-www-form-urlencoded
D/OkHttp: Content-Length: 26
D/OkHttp: title=vz&body=gde&userId=1
D/OkHttp: --> END POST (26-byte body)
D/OkHttp: <-- 200 OK http://mywebsite/404/
<html of my 404 page>
D/OkHttp: <-- END HTTP (2119-byte body)
It's giving me my servers' 404.html file as the response. but When I call the same API from POSTMAN it's giving me the desired result.
So basically I can access and get the result from POSTMAN but cannot consume it from Android app using retrofit
Any idea how to fix it? what can be the problem?