我想从我的代码或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);
}
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;
}
}
活动类别:
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();
}
});
}
答案 0 :(得分:0)
您几乎可以找到解决方案。但是,在将参数传递给API请求时,您几乎没有犯错。
从Insomia应用程序的屏幕截图中可以看到,该API需要JSONArray作为参数,但是您正在发送JSONObject。
示例JSON参数
Api.java
根据上述JSON结构,您需要将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()
方法参数中添加//......
// 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
}
});
。
在Activity或Fragment中使用上述方法,如下所示:
MapDetails
注意:请根据您的要求更改基本URL。
编辑:
将“活动”中的方法参数从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);
}
再次更改方法参数
{{1}}