我最近接手了一个使用Android的Room Persistence库以及Retrofit的项目。不幸的是,我对这两个库都不了解。
我目前正在尝试使用翻新功能将与Room一起保存的记录的JSON数组发送到API。
我的实体看起来像这样:
@Entity(tableName = "location_table")
public class LocationEntity implements Serializable {
@NonNull
@PrimaryKey(autoGenerate = true)
private int id;
@SerializedName("job_id")
@ColumnInfo(name = "job_id")
@Expose
private String job_id;
@SerializedName("coordinates")
@ColumnInfo(name = "coordinates")
@Expose
private String coordinates;
@SerializedName("created_at")
@ColumnInfo(name = "created_at")
@Expose
private String created_at;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getJob_id() { return job_id; }
public void setJob_id(String job_id) { this.job_id = job_id; }
public String getCoordinates() { return coordinates; }
public void setCoordinates(String coordinates) { this.coordinates = coordinates; }
public String getCreated_at() { return created_at; }
public void setCreated_at(String created_at) { this.created_at = created_at; }
@Ignore
public LocationEntity() {
// DATA TO IGNORE
}
public LocationEntity(String job_id, String coordinates, String created_at) {
this.job_id = job_id;
this.coordinates = coordinates;
this.created_at = created_at;
}
}
在我的DOA中,我有:
@Dao
public interface LocationDAO {
@Query("SELECT * from location_table ORDER BY id ASC")
List<LocationEntity> getAll();
}
存储库具有:
public class LocationRepository {
private static BudtrackDatabase budtrackDatabase;
public LocationRepository(Context context) {
budtrackDatabase = Room.databaseBuilder(context, BudtrackDatabase.class, AppConstants.DATABASE_NAME).build();
}
public static List<LocationEntity> getLocations() {
return budtrackDatabase.locationDAO().getAll();
}
}
我的要求:
public interface LocationRequest {
/**
* Send through multiple locations
* @param token
* @param coordinates
*/
@FormUrlEncoded
@POST("api/coordinates")
Call<String> sendLocations(@Field("token") String token,
@Field("coordinates[]") List<LocationEntity> coordinates);
}
然后我尝试像这样使用改造发送数据:
String token = "12345";
List<LocationEntity> locations = locationRepository.getLocations();
Call<String> callLocation = sendLocationService.sendLocations(token, locations);
callLocation.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
// Logic for success
} else {
try {
JSONObject jObjError = new JSONObject(response.errorBody().string());
Toast.makeText(activity, jObjError.getString("message"), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(activity, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e("Failed To Send Location", t.getMessage());
}
});
然后我像这样获取API端的数据:
"coordinates" : [
"com.yard8.budtrack.data.location.LocationEntity@4d87c17",
"com.yard8.budtrack.data.location.LocationEntity@6c60804",
"com.yard8.budtrack.data.location.LocationEntity@fdad1ed",
"com.yard8.budtrack.data.location.LocationEntity@66fc822",
"com.yard8.budtrack.data.location.LocationEntity@8dec2b3",
"com.yard8.budtrack.data.location.LocationEntity@f9da070",
];
通过数据发送以使其成为JSON数组的正确方法是什么?
答案 0 :(得分:1)
我可能一直在试图使事情变得过于复杂,而我要做的就是改变
@Field("coordinates[]") List<LocationEntity> coordinates
到
@Field("coordinates") String coordinates
然后在通话之前使用...
Gson gson = new Gson();
String locations = gson.toJson(locationRepository.getLocations());
...并将其作为我的第二个参数传递。
Call<String> callLocation = sendLocationService.sendLocations(token, locations);
由于我一直在接收数据服务器端的方式,我真的认为这仅适用于单个对象,而不适用于它们的列表。
答案 1 :(得分:0)
您尝试过类似的事情
1。创建新的班级
class ParentEntity{
private List<LocationEntity> locationEntities;
public List<LocationEntity> getLocationEntities() {
return locationEntities;
}
public void setLocationEntities(List<LocationEntity> locationEntities) {
this.locationEntities = locationEntities;
}
}
2。然后在您的响应改进中
// ....... //
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
// Logic for success
ParentEntity readObj = response.body();
List<LocationEntity> locations = readObj.getLocationEntities();
// DO SOMETHING YOU WANT
} else {
try {
JSONObject jObjError = new JSONObject(response.errorBody().string());
Toast.makeText(activity, jObjError.getString("message"), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(activity, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
// ....... //
更新
尝试使用@Body
,可能是下面的结构
1.创建新的课程
class ParentEntity{
private LocationEntity location;
private String token;
public LocationEntity getLocation() {
return location;
}
public void setLocation(LocationEntity location) {
this.location = location;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
使用正文代替字段,也无需使用@FormUrlEncoded
@POST(“ /”) 呼叫sendLocations(@Body ParentEntity parentEntity);