What is the possible error for this retrofit code? I can't understand the connection of my API and MOBILE

Viewed 164

I would like to find out the error is from my code or from my api.

This is my API CLASS:

      import retrofit2.Call;
      import retrofit2.http.Body;
      import retrofit2.http.POST;

      public interface Api {


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

          @POST("/api/Registration/RegisterDevice")
          Call<RegisterDetails> registerDetails(@Body RegisterDetails                     
           registerAllDetails);

      }

SETTER CLASS:

      import com.google.gson.annotations.SerializedName;

      public class MapDetails {
          @SerializedName("SerialNumber")
          private String serialNumber;
          @SerializedName("Coordinate1")
          private String coordinate1;
          @SerializedName("Coordinate2")
          private String coordinate2;
          @SerializedName("DateTime")
          private String dateTime;
          @SerializedName("Speed")
          private String speed;
          @SerializedName("Port")
          private Integer 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 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;
          }
      }

Activity Class:

                    MapDetails mapDetails = new MapDetails("1807200005", 
                    lat,lon, currentDateTimeString, "0", 9090);
                    setLocation(mapDetails);



                   private void setLocation(MapDetails mapDetails) {
                      initializeRetrofit(mapDetails);
                     }

                private void initializeRetrofit(MapDetails mapDetails) {
                    Retrofit.Builder builder = new Retrofit.Builder()
                            .baseUrl("http://undefine.apisecure.data[![enter image description here][1]][1]")
                      .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<MapDetails>() {
                        @Override
                        public void onResponse(Call<MapDetails> call, 
                         Response<MapDetails> response) {

                            Snackbar.make(view,""+ response, 
                                    Snackbar.LENGTH_INDEFINITE)
                                    .setAction("Action", null).show();
                        }

                        @Override
                        public void onFailure(Call call, Throwable t) {
                            Snackbar.make(view, ""+t.getMessage(), 
                                      Snackbar.LENGTH_INDEFINITE)
                                    .setAction("Action", null).show();

                        }
                    });


                }

On my app , this is the response on my app:enter image description here

but its not added to my sql. But if am using insomia, it would have send the data I've created to the database.enter image description here

this data was inserted via insomia not on mobile. enter image description here

1 Answers

You almost reached to the solution. But, you made little mistake while passing parameters to the API request.

As I can see from the screenshot of Insomia app, that API requires JSONArray as parameter but you're sending JSONObject.

Sample JSON parameter

[
    {
        "SerialNumber" : "1234",
        "Coordinate1" : "12.7845874",
        "Coordinate2" : "76.4584578",
        "DateTime" : "2018-11-14 08:45:00",
        "Speed" : "0",
        "Port" : 9090
    }
]

According to the above JSON structure you need to change the Api.java class to something like this:

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

import java.util.List;  // add import

public interface Api {

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

                                            //  ^^^^ changes here

    @POST("/api/Registration/RegisterDevice")
    Call < RegisterDetails > registerDetails(@Body RegisterDetails registerAllDetails);

}

Add List<MapDetails> to mapDetailLocation() method parameter.

And in Activity or Fragment use above method like this:

//......
// part of the code

MapDetails mapDetails = new MapDetails("1807200005", lat, lon, currentDateTimeString, "0", 9090);

List<MapDetails> data = new ArrayList<>();

data.add(mapDetails);

Retrofit.Builder builder = new Retrofit.Builder()
                            .baseUrl("<BASE_URL>")  // change base URL
                            .addConverterFactory(GsonConverterFactory.create());

Retrofit retrofit = builder.build();

Api locate = retrofit.create(Api.class);

Call<MapDetails> call = locate.mapDetailLocation(data);     // NOTICE THE CHANGES IN PARAMETER

call.enqueue(new Callback<MapDetails>() {
    @Override
    public void onResponse(Call<MapDetails> call, Response<MapDetails> response) {

        // do whatever you want

    }

    @Override
    public void onFailure(Call call, Throwable t) {

        // log the error message 

    }
});

Note: Please change base URL according to your requirement.

Edit:

Change method parameters in Activity from MapDetails to List<MapDetails>

// prepare data

MapDetails data = new MapDetails("1807200005", lat, lon, currentDateTimeString, "0", 9090);

// add it to ArrayList

List<MapDetails> mapDetails = new ArrayList<>();

mapDetails.add(data);

// pass it as an argument

private void setLocation(List<MapDetails> mapDetails) {
    initializeRetrofit(mapDetails);
}

Change method parameter in initializeRetrofit()

private void initializeRetrofit(List<MapDetails> mapDetails) {
    Retrofit.Builder builder = new Retrofit.Builder()
                            .baseUrl("<BASE_URL>")  // change base URL
                            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();

    Api locate = retrofit.create(Api.class);

    SetMapLocationApiCaller(locate, mapDetails);

}

Again change method parameter

private void SetMapLocationApiCaller(Api locate, List<MapDetails> mapDetails) {
    Call<MapDetails> call = locate.mapDetailLocation(mapDetails);
    executeCallAsynchronously(call);
}
Related