Reputation: 139
i am facing a problem regarding posting data in an array in android using retrofit 2. i have to post the data of nearly 14 fields in my profile setting activity ... Like this ...
name="basics[first_name] , name="basics[last_name]" , name="basics[phone_number]"
i have to send data in this format. i am not understanding how to do it need help.i am not understanding how to make Call of the api in the interface because i have to put data in an array. Currently i am doing it like this but i know its not right...
@FormUrlEncoded
@POST("profile_setting/basic_setting")
Call<ResponseBody> UpdateBasics(
@Query("user_id") int user_id ,
@Field("nickname") String nickname ,
@Field("first_name") String first_name ,
@Field("last_name") String last_name ,
@Field("phone_number") String phone_number ,
@Field("fax") String fax
);
Upvotes: 2
Views: 1842
Reputation: 2178
You can follow this was. (posting how i've done that)
@POST("Users.json")
Call<UploadData> uploadToken(@Body UploadData uploadData);
UploadData.class
public class UploadData {
private String DeviceToken, DeviceIMEI;
public UploadData(String deviceToken, String deviceIMEI) {
DeviceToken = deviceToken;
DeviceIMEI = deviceIMEI;
}
public String getDeviceToken() {
return DeviceToken;
}
public String getDeviceIMEI() {
return DeviceIMEI;
}
}
Then in your Activity
private void uploadToken() {
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
UploadData uploadToken = new UploadData(deviceToken, imei);
final Call<UploadData> callUpload = apiInterface.uploadToken(uploadToken);
callUpload.enqueue(new Callback<UploadData>() {
@Override
public void onResponse(Call<UploadData> call, Response<UploadData> response) {
if (response.isSuccessful()) {
Toasty.success(Main.this, "Token Uploaded !! ", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure (Call < UploadData > call, Throwable t){
call.cancel();
Toasty.error(Main.this, "Error: " + t.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
}
});
}
Upvotes: 0
Reputation: 304
Make a class
public class Basic {
public final int user_id;
public final String nickname;
....
public Basic(int user_id, ...) {
}
}
Then pass list of objects of this class to this interface
public interface MyService {
@POST("/basic")
Response void sendData(@Body List<Basic> basic);
}
Or you can do the same with JSONObject. Just make a list of jsonobjects
JSONObject paramObject = new JSONObject();
paramObject.put(value_one, "field_one"));
paramObject.put(value_second, "field_second"));
put the objects in a list
list.add(paramObject);
then pass to the retrofit
public interface MyService {
@POST("/basic")
Response void sendJsonObjectData(@Body List<JSONObject> basic);
}
Upvotes: 2
Reputation: 11467
Do this way to send Json
Object as request parameters using Retrofit 2
@Headers("Content-Type: application/json")
@POST("profile_setting/basic_setting")
Call<ResponseBody> UpdateBasics(@Body String body);
This is how you would use the above method to send json object
try {
JSONObject paramObject = new JSONObject();
paramObject.put(value_one, "field_one"));
paramObject.put(value_second, "field_second"));
Call<ResponseBody> userCall = apiInterface.UpdateBasics(paramObject.toString());
userCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//handle your result here
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
//handle failure
}
});
} catch (JSONException e) {
e.printStackTrace();
}
Upvotes: 0