这个改造代码可能的错误是什么?我无法理解我的 API 和 MOBILE 的连接

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

我想找出错误是来自我的代码还是来自我的 api。

这是我的 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();

                        }
                    });


                }

在我的应用程序中,这是我的应用程序的响应:

但它没有添加到我的 sql。但是如果我正在使用 insomia,它会将我创建的数据发送到数据库。

此数据是通过 insomia 而非移动设备插入的。

您几乎找到了答案。但是,您在将参数传递给 API 请求时犯了一点错误。

正如我从 Insomia 应用程序的屏幕截图中看到的那样,API 需要 JSONArray 作为参数,但您发送的是 JSONObject.

示例JSON参数

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

根据上面的 JSON 结构,您需要将 Api.java class 更改为如下内容:

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

}

List<MapDetails> 添加到 mapDetailLocation() 方法参数。

并且在 Activity 或 Fragment 中使用上面的方法是这样的:

//......
// 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 

    }
});

注意:请根据您的要求更改基数URL。

编辑:

将 Activity 中的方法参数从 MapDetails 更改为 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);
}

更改 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);

}

再次更改方法参数

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