我在通过API解析数据时遇到问题。我的改造代码在响应中发送了真实状态,但是如果我寻找ServerDb,响应就不会出现。
API:
@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;
}
}
RetrofitClient:
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();
}
});
这里的响应为true表示事务为true ..但是在我的服务器数据库中,没有数据获取.....
我的api也是这样:
if I send that, i will having a response from my serverDb...
答案 0 :(得分:0)
@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);
}
TO
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();
}
});