我已经尝试过提出请求,但我不能这样做。
这是我正在使用的Retrofit界面。 通过会话调用,我正在进行基本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) {
}
});
答案 0 :(得分:1)
您可以查看Traccar Manager的原生版本,例如:
https://github.com/tananaev/traccar-manager-android/tree/native
它使用Retrofit。这是界面:
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);
}