This is my first time using retrofit and I am getting lost in the instructions. I read the official guides and quite a few other answers and tutorials but I am still having trouble. It seems like they all cover complex scenarios but not just the simple post for one field.
This is the details of what I am trying to do. Post a name to a php server:
Type: application/x-www-form-urlencoded Payload name: e.g. name=John%20Smith
I have spent quite a bit of time and have the basic but I think I am missing somthing:
I have worked out that I need an interface like this:
public interface ApiService {
@FormUrlEncoded
@POST("./")
Call<User> updateUser(@Field("Name") String name);
}
Then i have a model for my user:
public class User {
@SerializedName("Name")
String name;
}
Then I have my retrofit client:
public class RetroClient {
private static final String ROOT_URL = "http://alarm.abc.ch/";
public static ApiService RETROFIT_CLIENT;
public static ApiService getInstance() {
//if REST_CLIENT is null then set-up again.
if (RETROFIT_CLIENT == null) {
setupRestClient();
}
return RETROFIT_CLIENT;
}
private static void setupRestClient() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RETROFIT_CLIENT = retrofit.create(ApiService.class);
}
}
and I call it like this in the post function in my main activity:
public void post (){
mRetrofitCommonService = RetroClient.getInstance();
call = mRetrofitCommonService.updateUser("test");
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
System.out.println("responseee " + response);
}
@Override
public void onFailure(Call<User> call, Throwable t) {
System.out.println("eeee " + t);
}
});
And it is from there that I am totally lost. I am sure it is something small I am missing but not sure what.
Thanks in advance for your help.
EDIT: The code has been changed to reflect my current attempt. This is giving me the following error:
MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 2 path