Reputation: 1160
I'm new to Retrofit. I simply add a row to my database with using retrofit but i used field parameters in my api interface like this.
Call<String> matchWith(@Field("fbid") String fbid, @Field("matchwith") String matchwith);
It is hard to write for big objects so i decided to use @Body
annotation. But now its posting empty row/object in my mysql database. (With @Field
annotation in api interface my code inserts the data correctly)
Here is my code
@POST("/insertuser.php")
Call<User> postUser(@Body User user);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://myurl.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
Connect connect = retrofit.create(Connect.class);
Call<User> call = connect.postUser(user);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
}
@Override
public void onFailure(Call<User> call, Throwable t) {
}
});
In my object class i used @SerializedName
and @Expose
retrofit still inserts empty I have tried with just @SerilizedName
and just with @Expose
after that I used nothing but I got same result.
Upvotes: 1
Views: 638
Reputation: 427
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient=new OkHttpClient.Builder();
httpClient.addInterceptor(logging);**
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
}
Now you can see if you send the correct data and server responses correctly, otherwise search the problem in server
Upvotes: 1
Reputation: 1543
There is difference between @Field
and @Body
annotations. Your API might be accepting data as form-data
only. That is why your data is considered as empty data when you send it through request body i.e. using @Body
annotation.
You can change your web API to accept data as request body and then use as your are using.
Upvotes: 2