简体   繁体   中英

retrofit response is true but no data fetch in serverDB

I have problem in parsing data thru my API. my retrofit code was sending true state on my response but if i look for my ServerDb, the response not come.

API:

         @POST("/api/Database/NewLocation")
                Call<MapDetails> mapDetailLocation(@Body MapDetails 
mapDetails);

Map Details:

    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();

                        }
                    });

The response here is true means the transaction is true.. but in my server db, no data fetched.....

also my api is this:

在此处输入图片说明

    if I send that, i will having a response from my serverDb...
     @POST("api/Database/NewLocation")
     Call<ServerResponse> mapDetailLocation(@Body MapDetails mapDetails);

Add this class in your code to get Response from server.

ServerResponse.java class

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;
}

}

Change this

private static  final  String BASE_URL="http://58.69.149.164:9114/";

in initializeRetrofit too

Retrofit.Builder builder = new Retrofit.Builder().baseUrl("http://58.69.149.164:9114/")

And this

 Call<MapDetails> call = 
               locate.mapDetailLocation(mapDetails);
                    executeCallAsynchronously(call);
                }

TO

 Call<ServerResponse> call = 
           locate.mapDetailLocation(mapDetails);
                executeCallAsynchronously(call);
            }

Chnage your executeCallAsynchronously as

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();

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM