我想找出错误是来 self 的代码还是来 self 的 api。
这是我的 API 类:
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);
}
二传手类:
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;
}
}
事件类:
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();
}
});
}
请您参考如下方法:
您几乎找到了解决方案。但是,您在将参数传递给 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类是这样的:
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。
编辑:
从 MapDetails 更改 Activity 中的方法参数至 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);
}


