Reputation: 12316
i have implement a Observer/Observable pattern in android application, but when do the notify, receivers dont get the call.
This is my code:
The observer, is a class for call a rest service, if the call fails because the server is off, or other problem, i change the rest server ip, increase a int value from Observable and Observable must call my update override method, but dont do it:
public class LoginApiCall extends ApiCallBase<Object> implements Observer {
private String team;
private String password;
private String deviceId;
private LoginIntents loginIntents;
public LoginApiCall(Context context, String team, String password, String deviceId, ApiResponseListener listener) {
super(context, listener);
this.team = team;
this.password = password;
this.deviceId = deviceId;
loginIntents = LoginIntents.getInstance();
loginIntents.addObserver(this);
}
@Override
protected void doWork() {
LoginData login = new LoginData(team, password, deviceId);
Gson gson = new Gson();
String value = gson.toJson(login);
try {
ByteArrayEntity entity = new ByteArrayEntity(value.getBytes("UTF-8"));
Api.post(context, "teamLogin", entity, new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject jsonObject) {
apiSuccess(jsonObject);
}
@Override
public void onFailure(Throwable throwable, JSONObject jsonObject) {
throwable.printStackTrace();
apiError(new ApiException());
}
@Override
public void onFailure(Throwable throwable, String content) {
if (throwable.getCause() instanceof ConnectTimeoutException) {
System.out.println("Connection timeout !");
}
loginIntents.increaseIntents();
throwable.printStackTrace();
apiError(new ServerException());
}
});
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Override
public void update(Observable observable, Object data) {
execute();
}
}
and this is the Observable, that when increase the intents attribute from 0 to 1, notify the observers and they should be do something:
public class LoginIntents extends Observable {
private static volatile LoginIntents instance = null;
private int intents = 0;
private LoginIntents() {
}
public static LoginIntents getInstance() {
if (instance == null) {
instance = new LoginIntents();
}
return instance;
}
public int getIntents() {
return instance.intents;
}
public void increaseIntents() {
instance.intents++;
if (intents == 1) {
notifyObservers();
setChanged();
}
}
}
Whats wrong? the notifyObservers method is called when i debug and the setChanged too, but dont enter in update method of Observer..
Upvotes: 0
Views: 743