I am trying to communicate with my php web service (Google App Engine), but it keeps responding with body as null and also triggering the failure() method of Retrofit (callback).
I get the following error message in failure(), when debugging it:
retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
First here is the code where I trigger the request to my web service:
WebServiceManager.getInstance().registerRequest(email, password, username, firstname, lastname, new Callback<RegisterResponse>() {
@Override
public void success(RegisterResponse registerResponse, Response response) {
Toast.makeText(context, registerResponse.getToken() + "", Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(context,"error ", Toast.LENGTH_LONG).show();
}
});
And here is my WebServiceManager class for managing the requests and responses: http://pastebin.com/Q8bzqQ0M
And the model classes for the requests and responses: http://pastebin.com/aB98Yiua
This is how my json request looks like:
{
data{
"email": "regd@rgersf.dk",
"firstname": "regdrgersf",
"lastname": "regdrgersf",
"password": "hejhejhej",
"username": "regdrgersf"
}
}
I also think it is relevant to include the part where I receive the request on my web service:
<?php
require '../functions/db.php';
$data = getJsonRequest();
$email = $data['data']['email'];
$password = $data['data']['password'];
$username = $data['data']['username'];
$firstname = $data['data']['firstname'];
$lastname = $data['data']['lastname'];
$facebookID = $data['data']['facebookID'];
$dbClass = new DBClass();
$result = $dbClass->register($email, $password, $username, $firstname, $lastname, $facebookID);
$response = array();
if ($result == $dbClass->error_input || $result == $dbClass->error_email_exists || $result == $dbClass->error_unknown || $result == $dbClass->error_db_connection) {
$response['status'] = false;
$response['error'] = $result;
} else {
$response['status'] = true;
$response['token'] = $result;
}
sendJsonResponse($response);
?>
And the function "sendJsonResponse($response)" for sending the response:
function sendJsonResponse($response) {
header('HTTP/1.1 200 OK');
header('Content-type: application/json');
echo json_encode($response);
}
I can't figure out if the problem is somewhere in my Java (Android) code and retrofit or if I'm doing something wrong on the webserver.
I found a way to fix it myself, so I think I should post my solution here so that it might help others.. even though I am not sure why my first solution didn't work. By removing the annotation @FormUrlEncoded and changing @Field('data') to @Body, it actually worked. Så the interface looks like this now.
private interface webServiceInterface {
@POST(WebServiceManager.REQUEST_REGISTER)
void register(@Body RegisterRequest registerRequest, Callback<RegisterResponse> response);
}
Anyways, I really appreciate all the help I got from people here.