我面临有关使用翻新2在android中的数组中发布数据的问题。我必须在个人资料设置活动中发布近14个字段的数据... 像这样...
name="basics[first_name] , name="basics[last_name]" , name="basics[phone_number]"
我必须以这种格式发送数据。我不知道该怎么做需要帮助。我不了解如何在接口中调用api,因为我必须将数据放入数组中。 目前,我正在这样做,但是我知道它不正确...
@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
);
答案 0 :(得分:0)
以这种方式使用Json
将Retrofit 2
对象作为请求参数发送
@Headers("Content-Type: application/json")
@POST("profile_setting/basic_setting")
Call<ResponseBody> UpdateBasics(@Body String body);
这就是您使用上述方法发送json对象的方式
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();
}
答案 1 :(得分:0)
上课
public class Basic {
public final int user_id;
public final String nickname;
....
public Basic(int user_id, ...) {
}
}
然后将此类对象的列表传递给此接口
public interface MyService {
@POST("/basic")
Response void sendData(@Body List<Basic> basic);
}
或者您可以对JSONObject进行相同的操作。只需列出jsonobjects
JSONObject paramObject = new JSONObject();
paramObject.put(value_one, "field_one"));
paramObject.put(value_second, "field_second"));
将对象放入列表
list.add(paramObject);
然后通过改造
public interface MyService {
@POST("/basic")
Response void sendJsonObjectData(@Body List<JSONObject> basic);
}
答案 2 :(得分:0)
您可以按照此操作。 (发布我的操作方式)
@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;
}
}
然后在您的活动中
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();
}
});
}