android - 改造响应为真,但 serverDB 中没有数据获取
问题描述
我在通过我的 API 解析数据时遇到问题。我的改造代码在我的响应中发送了真实状态,但是如果我寻找我的 ServerDb,响应不会出现。
接口:
@POST("/api/Database/NewLocation")
Call<MapDetails> mapDetailLocation(@Body MapDetails
mapDetails);
地图详情:
public class MapDetails {
private String serialNumber;
private String coordinate1;
private String coordinate2;
private String dateTime;
private String speed;
private Integer Port;
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
public String getCoordinate1() {
return coordinate1;
}
public void setCoordinate1(String coordinate1) {
this.coordinate1 = coordinate1;
}
public String getCoordinate2() {
return coordinate2;
}
public void setCoordinate2(String coordinate2) {
this.coordinate2 = coordinate2;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
public String getSpeed() {
return speed;
}
public void setSpeed(String speed) {
this.speed = speed;
}
public Integer getPort() {
return Port;
}
public void setPort(Integer port) {
Port = port;
}
public MapDetails(String serialNumber, String coordinate1, String
coordinate2, String dateTime, String speed, Integer port) {
this.serialNumber = serialNumber;
this.coordinate1 = coordinate1;
this.coordinate2 = coordinate2;
this.dateTime = dateTime;
this.speed = speed;
Port = port;
}
}
改造客户:
public class RetrofitClient {
private static final String BASE_URL="http://58.69.149.164:9114";
private static RetrofitClient mInstance;
private Retrofit retrofit;
private RetrofitClient() {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static synchronized RetrofitClient getmInstance(){
if(mInstance==null){
mInstance=new RetrofitClient();
}
return mInstance;
}
public Api getApi(){
return retrofit.create(Api.class);
}
activity:
MapDetails mapDetails = new MapDetails(gg, lat, lon,
`enter code here`currentDateTimeString, "0", 9090);
setLocation(mapDetails);
DatabaseHelper databaseHelper = new
DatabaseHelper(getApplicationContext());
SQLiteDatabase db =
databaseHelper.getWritableDatabase();
boolean accepted =
databaseHelper.saveLocationToLocalDatabase(lat, lon,
currentDateTimeString, syncPassed, db);
if (accepted == true)
Snackbar.make(view, "Location Found", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
else
Snackbar.make(view, "Error Location", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
private void setLocation(MapDetails mapDetails) {
initializeRetrofit(mapDetails);
}
private void initializeRetrofit(MapDetails mapDetails) {
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://58.69.149.164:9114")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
Api locate = retrofit.create(Api.class);
SetMapLocationApiCaller(locate, mapDetails);
}
private void SetMapLocationApiCaller(Api locate, MapDetails
mapDetails) {
Call<MapDetails> call =
locate.mapDetailLocation(mapDetails);
executeCallAsynchronously(call);
}
private void executeCallAsynchronously(Call call) {
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response)
{
Toast.makeText(NavDrawerFleet.this, ""+response,
Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call call, Throwable t) {
Toast.makeText(NavDrawerFleet.this,
t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
这里的响应是真的意味着交易是真的..但是在我的服务器数据库中,没有获取数据.....
我的api也是这样的:
if I send that, i will having a response from my serverDb...
解决方案
@POST("api/Database/NewLocation")
Call<ServerResponse> mapDetailLocation(@Body MapDetails mapDetails);
在您的代码中添加此类以从服务器获取响应。
ServerResponse.java 类
public class ServerResponse {
@SerializedName("Status")
@Expose
private Integer status;
@SerializedName("Message")
@Expose
private String message;
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
改变这个
private static final String BASE_URL="http://58.69.149.164:9114/";
在initializeRetrofit
太
Retrofit.Builder builder = new Retrofit.Builder().baseUrl("http://58.69.149.164:9114/")
和这个
Call<MapDetails> call =
locate.mapDetailLocation(mapDetails);
executeCallAsynchronously(call);
}
至
Call<ServerResponse> call =
locate.mapDetailLocation(mapDetails);
executeCallAsynchronously(call);
}
更改您的executeCallAsynchronously
身份
private void executeCallAsynchronously(Call call) {
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response)
{
ServerResponse res = response.body();
int status = res.getStatus();
String msg = res.getMessage();
}
@Override
public void onFailure(Call call, Throwable t) {
Toast.makeText(NavDrawerFleet.this,
t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
推荐阅读
- delphi - Delphi $E 指令
- python - 数据框 pandas 中的几个 � 字符
- sql - 如何在 SQL 中重复多个值的日期并填充缺失的日期
- angular9 - 如何在 angular8 中使用 vtk.js
- pyspark - 运行 ./pyspark 找不到本地目录
- docusignapi - DocuSign - 如果我们的客户为 DocuSign 启用了 SSO,如何处理
- c++ - 什么可能导致Ncurses混乱中的坐标
- python - 关于将 sum() 与 zip() 结合起来的问题
- asp.net-mvc - Azure App Service 授权/easy auth 与 ASP.NET Forms auth 冲突
- angular - 如何使用 window.opener 从 Angular 8 中的子窗口获取父组件数据