我试过提出这个要求,但我做不到。
这是我正在使用的改装接口。
通过会话调用,我正在执行基本的http身份验证。
public interface APIService {
@POST("/users")
@FormUrlEncoded
Call<Users> saveUserPost(@Header("Authorization") String credentials,
@Field("admin") Boolean admin,
@Field("email") String email,
@Field("name") String name,
@Field("password") String password);
@POST("/session")
Call<List<Session>> openSession(@Body Session session);
}
这是我活动的oncreate方法,在它中,我尝试登录并验证用户是否已注册。
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://http://192.168.1.72:8082/api/")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
APIService apiService = retrofit.create(APIService.class);
Session session = new Session();
session.setPassword("admin");
session.setEmail("admin");
Call<List<Session>> call = apiService.openSession(session);
call.enqueue(new Callback<List<Session>>() {
@Override
public void onResponse(Call<List<Session>> call, Response<List<Session>> response) {
switch (response.code()) {
case 200:
Toast.makeText(getApplicationContext(), "error = 200", Toast.LENGTH_LONG).show();
break;
case 400:
Toast.makeText(getApplicationContext(), "error = 400", Toast.LENGTH_LONG).show();
break;
case 401:
Toast.makeText(getApplicationContext(), "error = 401", Toast.LENGTH_LONG).show();
break;
case 404:
Toast.makeText(getApplicationContext(), "error = 404", Toast.LENGTH_LONG).show();
break;
case 405:
Toast.makeText(getApplicationContext(), "error = 405", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getApplicationContext(), ""+response.isSuccessful(), Toast.LENGTH_LONG).show();
break;
}
}
@Override
public void onFailure(Call<List<Session>> call, Throwable t) {
}
});
最佳答案
您可以查看traccar manager的本机版本,例如:
https://github.com/tananaev/traccar-manager-android/tree/native
它使用改装。界面如下:
public interface WebService {
@FormUrlEncoded
@POST("/api/session")
Call<User> addSession(@Field("email") String email, @Field("password") String password);
@GET("/api/devices")
Call<List<Device>> getDevices();
@GET("/api/commandtypes")
Call<List<CommandType>> getCommandTypes(@Query("deviceId") long deviceId);
@POST("/api/commands")
Call<Command> sendCommand(@Body Command command);
}