I am using Retrofit in my android app to communicate with my server. In one of my server calls I am expecting a String response from server. So, I declare a callback which expects a string value. Callback<String>
. In the php, I echo a string. Say echo "test";
When I hit the url in browser, the echo works as expected test
. But in my android app, failure callback is called. I tried changing the php to echo "\"test\"";
On browser : "test"
On android : success callback is called.
I solved it by declaring a variable. Php:
$result = "test";
echo $result;
Browser: test
Android : success callback is called.
My question is, is this how Retrofit works? Or am I doing anything wrong? Also, to solve this is there any way other than declaring a variable?
Callback<String>
does not make a lot of sense in the context of retrofit. By default retrofit operates using GSON.
What you are actually waiting for from the server is json deserialized into a POJO (simple java object).
Lets say you have a data model (POJO) like:
public class User {
public final String name;
}
Then you'd use a callback like this Callback<User>
. And from the server you should do: echo '{ "name" : "Simon" }';
In your success callback you will have an instance of User class with the name field set to 'Simon'.
More on this here: http://square.github.io/retrofit/