2017-06-15 45 views
1

我有我的代码问题。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; 
} 

}

+0

你可以使用接口,你试过吗? – Ashwani

+0

虽然他们在Retrofit的线程上运行,但无法在JavaFX上的我的控制器上运行:( –

回答

1

做这种方式

public void connectPassword(String nameuser, String password, MyInterface myInterface) { 
    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(); 
      myInterface.onSuccess(true); 
      //return boolean her 
     } 

     public void onFailure(Call<Connection> call, Throwable throwable) { 
      System.out.print(throwable.getMessage()); 
      myInterface.onException(throwable.getMessage()); 
      // throw Exception her 
     } 
    }); 
} 

MyInterface.class

public interface MyInterface{ 
public void onSuccess(boolean value); 
public void onException(String value); 
} 

从您的调用类下设主要

public class Main implements MyInterface{ 

@Override 
public void onSuccess(boolean value){ 
//TODO do your stuff with returned boolean from your call 
} 

@Override 
public void onException(String value){ 
//TODO you will receive the message here 
} 

//in your onCreate 
... 
//method call 
connectPassword("username","password",this); 

} 

希望这有助于。

+0

完美我使类似的东西,并完美地工作 关心 –

0

恐怕你不能简单地从改造回调中返回。它们是异步的,因此你也可以异步获取你的值。

然而,你可以做的是用你自己的回调传播价值。这将导致所谓的“回拨地狱”,但它的工作原理,如果你不是太困扰它,它会没事的。

因此,让我尝试并引导您通过可能的解决方案(有几种方法可以做到这一点)。

说,你有你的回调,像这样定义的:

public interface UserCallbacks { 
    void onUserCreatedSuccessfully(@NonNull User user); 

    void onError(@NonNull Throwable throwable); 
} 

非常简单的界面,公开2种方法,你可以调用。第一个告诉听众用户已成功创建,另一个告诉听众有错误。

现在你改变你的方法是这样的:

public void connectPassword(String nameuser, String password, UserCallbacks callbacks) { 
    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) { 
     callbacks.onUserCreatedSuccessfully(/*send the user here*/); 
    } 

    public void onFailure(Call<Connection> call, Throwable throwable) { 
     callbacks.onError(throwable); 
    } 
    }); 
} 

的最后一步是通过回调对象。假设你的控制器实现了回调接口,你可以这样做:

public class UserController implements UserCallbacks { 
    // ... 
    public void onUserCreatedSuccessfully(@NonNull User user) { 
    // do whatever we need to do 
    } 

    public void onError(@NonNull Throwable throwable) { 
    // inspect the error and see what we have to change in the ui 
    } 

    public void connect(String username, String password) { 
    // Suppose you have your service created 
    service.connectPassword(username, password. this); 
    } 
} 

只要确保你在UI线程中运行任何UI代码。可以使用runOnUiThread