2017-06-15 8 views
1

私のコードに問題があります。onResponse変数を返すか例外をスローするRetrofit 2.0

私は変数または例外を返すようにしたいが、私はstackoverflowの別の投稿のmethodeを理解していない。

誰かが私に解決策を説明できる場合、私は改造 との新たなんだ、それは私はそれが私が希望MVC

での私のモデルのクラスのユーザーに、このインスタンスのMethodeのを使用するプロジェクトのために

です私のユーザーが作成し、例外の場合にコネクションがダウン

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

であればブール変数を返します0

}

そして、私のクラスの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

私はJavaFXのコントローラーで動作できないRetrofitのスレッドで実行しましたが、( –

答えて

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

私は、改造コールバックから単純に戻ることはできません。これらは非同期なので、値も非同期的に取得されます。

ただし、できることは、独自のコールバックで値を伝播していることです。これはいわゆる "コールバック地獄"につながりますが、それはうまくいきます。

私は可能な解決策を試してみましょう(これを行うにはいくつかの方法があります)。あなたが呼び出すことができる2つの方法を公開し

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

    void onError(@NonNull Throwable throwable); 
} 

非常にシンプルなインターフェイス:

はあなたのコールバックがそうのように定義されていると言います。まず最初に、ユーザーが正常に作成されたことをリスナーに通知し、もう1つはリスナーにエラーがあったことを通知します。

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

関連する問題