我有我的代码问题。onResponse返回变量或抛出异常Retrofit 2.0
我想返回变量或异常,但我不明白在stackoverflow不同的帖子中的方法。
我是新与改造 如果有人能向我解释一个解决方案,它是一个项目
我在一类用户使用这个实例梅索德这是我在MVC模式
我想返回布尔变量时,我的用户是创建和异常,如果接头是向下
public void connectPassword(String nameuser, String password) {
Api api = new Api();
Call<Connection> call = api.getService().connectPassWord(nameuser, password);
call.enqueue(new Callback<Connection>() {
public void onResponse(Call<Connection> call, Response<Connection> response) {
System.out.println(response.body().getToken());
System.out.println("Connecter");
User.this.token = response.body().getToken();
//return boolean her
}
public void onFailure(Call<Connection> call, Throwable throwable) {
System.out.print(throwable.getMessage());
// throw Exception her
}
});
}
我班ApiService
public interface ApiService {
@FormUrlEncoded
@POST("createuser.php")
Call<Register> sendRegister(@Field("username") String username, @Field("password") String password, @Field("email") String email);
@FormUrlEncoded
@POST("login.php")
Call<Connection> connectPassWord(@Field("username") String username, @Field("password") String password);
@FormUrlEncoded
@POST("login.php")
Call<Connection> connectToken(@Field("username") String username, @Field("token") String password);
}
我的类的API
public class Api {
private Retrofit retrofit;
private ApiService service;
public Api(){
this.retrofit = new Retrofit.Builder().baseUrl("http://localhost/ProjetS2/").addConverterFactory(new NullOnEmptyConverterFactory()).addConverterFactory(GsonConverterFactory.create()).build();
this.service = retrofit.create(ApiService.class);
}
public ApiService getService(){
return this.service;
}
}
你可以使用接口,你试过吗? – Ashwani
虽然他们在Retrofit的线程上运行,但无法在JavaFX上的我的控制器上运行:( –