【发布时间】:2019-02-12 08:07:18
【问题描述】:
我最近接手了一个使用 Android 的 Room 持久性库以及 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 数组通过的正确方法是什么?
【问题讨论】:
-
你遇到了什么问题?
-
我在 API 上收到的数据似乎只是一个字符串,例如“com.yard8.budtrack.data.location.LocationEntity@4d87c17”,而不是具有坐标等属性的实际对象和 created_at
-
您是否尝试过在 sendLocations() 函数中使用 ArrayList 而不是“List”?
-
在从 LocationDAO 编译时尝试使用 ArrayList 会引发错误,提示“错误:不确定如何将游标转换为此方法的返回类型”
-
你试过 ArrayList
吗?
标签: android retrofit2 android-room