Last active
August 29, 2015 14:00
-
-
Save rfermontero/11343860 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Api { | |
private static String BASE_URL = ""; | |
private static final int DEFAULT_TIMEOUT = 30 * 1000; | |
private static AsyncHttpClient client = new AsyncHttpClient(); | |
static { | |
client.setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120427 Firefox/15.0a1"); | |
client.setTimeout(DEFAULT_TIMEOUT); | |
} | |
public static void get(String relativeUrl, RequestParams params, AsyncHttpResponseHandler responseHandler) { | |
final String absoluteUrl = getAbsoluteUrl(relativeUrl); | |
client.get(absoluteUrl, params, responseHandler); | |
} | |
public static void post(String relativeUrl, RequestParams params, AsyncHttpResponseHandler responseHandler) { | |
final String absoluteUrl = getAbsoluteUrl(relativeUrl); | |
client.post(absoluteUrl, params, responseHandler); | |
} | |
public static void post(Context context, String relativeUrl, HttpEntity entity, AsyncHttpResponseHandler responseHandler) { | |
final String absoluteUrl = getAbsoluteUrl(relativeUrl); | |
client.post(context, absoluteUrl, entity, "application/json", responseHandler); | |
} | |
public static void put(Context context, String relativeUrl, HttpEntity entity, AsyncHttpResponseHandler responseHandler) { | |
final String absoluteUrl = getAbsoluteUrl(relativeUrl); | |
client.put(context, absoluteUrl, entity, "application/json", responseHandler); | |
} | |
public static void put(String relativeUrl, RequestParams params, AsyncHttpResponseHandler responseHandler) { | |
final String absoluteUrl = getAbsoluteUrl(relativeUrl); | |
client.put(absoluteUrl, params, responseHandler); | |
} | |
private static String getAbsoluteUrl(String url) { | |
BASE_URL = Utils.getServers().get("FirstServer"); | |
String urlWithSpces = BASE_URL + url; | |
return urlWithSpces.replaceAll(" ", "%20"); | |
} | |
} | |
/****************/ | |
public abstract class ApiCallBase<T> { | |
private ProgressDialog dialog; | |
protected Context context; | |
private ApiResponseListener listener; | |
protected boolean dialogEnabled; | |
public ApiCallBase(Context context, ApiResponseListener listener) { | |
this.context = context; | |
this.listener = listener; | |
this.dialogEnabled = true; | |
} | |
public void execute() { | |
onPreExecute(); | |
doWork(); | |
} | |
protected abstract void doWork(); | |
protected void apiError(ApiException e) { | |
if (this.dialogEnabled && !isReTry) { | |
this.dialog.dismiss(); | |
} | |
this.listener.error(e); | |
} | |
protected void apiSuccess(T result) { | |
if (this.dialogEnabled) { | |
this.dialog.dismiss(); | |
} | |
this.listener.success(result); | |
} | |
private void onPreExecute() { | |
if (this.dialogEnabled && !isReTry) { | |
this.dialog = new ProgressDialog(context); | |
this.dialog.setMessage("Conectando con el servidor..."); | |
this.dialog.show(); | |
} | |
} | |
} | |
public class PostData xtends ApiCallBase<Object> { | |
private String tdata; | |
public PostData(Context context, String data ApiResponseListener listener) { | |
super(context, listener); | |
this.data = data | |
this.dialogEnabled = false;//Si lo pones true sale el alertdialog, false, lo oculta | |
} | |
@Override | |
protected void doWork() { | |
AlertData alertData = new AlertData(team, password, idClient, workText, alertId); | |
Gson gson = new Gson(); | |
String value = gson.toJson(alertData); | |
try { | |
StringEntity entity = new StringEntity(value.toString()); | |
Api.post(context, "adviceInit", entity, new JsonHttpResponseHandler() { | |
@Override | |
public void onSuccess(JSONObject jsonObject) { | |
//Maneja el resultado del JsonObject como mas te parezca | |
//Despues llama a apiSuccess(result) para enviar el resultado al handler superior | |
apiSuccess(result); | |
} | |
@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 !"); | |
} | |
throwable.printStackTrace(); | |
apiError(new ApiException()); | |
} | |
}); | |
} catch (UnsupportedEncodingException e) { | |
apiError(new ApiException()); | |
} | |
} | |
public class AlertData { | |
private String data; | |
public AlertData(String tdata { | |
this.data = data | |
} | |
} | |
} | |
public class SyncAlert extends Observable { | |
private Alert alert; | |
private Activity activity; | |
private AlertDialog dialog; | |
private boolean isRunning = false; | |
public SyncAlert(Alert alert, Activity activity, Observer observer) { | |
this.alert = alert; | |
this.activity = activity; | |
addObserver(observer); | |
sendAlert(); | |
} | |
private void sendAlert() { | |
if (!isRunning) { | |
try { | |
onPreExecute(); | |
openAlert(); | |
} catch (Exception e) { | |
stopRunning(); | |
} | |
} | |
} | |
private void openAlert() { | |
PostAlert postAlert = new PostAlert(activity, alert.getData() new ApiResponseListener() { | |
@Override | |
public void success(Object value) { | |
Log.d(activity.getString(R.string.aa), activity.getString(R.string.alert_sync_ok)); | |
sendSign(); | |
} | |
@Override | |
public void error(ApiException e) { | |
stopRunning(); | |
Toast.makeText(activity, e.getErrorMessage(), Toast.LENGTH_LONG).show(); | |
} | |
}); | |
postAlert.execute(); | |
} | |
private void sendSign() { | |
Log.d(activity.getString(R.string.aa), "Envio de firma"); | |
PostSignApiCall postSign = new PostSignApiCall(alert.getSignPath(), activity, new ApiResponseListener() { | |
@Override | |
public void success(Object value) { | |
Log.d(activity.getString(R.string.aa), activity.getString(R.string.send_sign_ok)); | |
sendPhotos(); | |
} | |
@Override | |
public void error(ApiException e) { | |
stopRunning(); | |
Toast.makeText(activity, e.getErrorMessage(), Toast.LENGTH_LONG).show(); | |
} | |
}); | |
postSign.execute(); | |
} | |
private void sendPhotos() { | |
List<String> paths = new ArrayList<String>(); | |
if (alert.getPhotoList().split("::").length > 0 && !alert.getPhotoList().split("::")[0].equals("")) { | |
paths = Arrays.asList(alert.getPhotoList().split("::")); | |
} | |
final Integer size = paths.size(); | |
if (size == 0) { | |
finishAlert(); | |
} | |
for (String photoPath : paths) { | |
if (photoPath != null && photoPath.length() > 0) { | |
PostPhotoApiCall postPhoto = new PostPhotoApiCall(photoPath, activity, new ApiResponseListener() { | |
@Override | |
public void success(Object value) { | |
Log.d(activity.getString(R.string.aa), activity.getString(R.string.photo_send_ok)); | |
PostActionResponse response = new Gson().fromJson(value.toString(), PostActionResponse.class); | |
try { | |
if (Integer.parseInt(response.getMessage()) == size) { | |
finishAlert(); | |
} | |
} catch (Exception generalEx) { | |
stopRunning(); | |
} | |
} | |
@Override | |
public void error(ApiException e) { | |
stopRunning(); | |
Toast.makeText(activity, e.getErrorMessage(), Toast.LENGTH_LONG).show(); | |
} | |
}); | |
postPhoto.execute(); | |
} | |
} | |
} | |
private void finishAlert() { | |
PostAlertFinish finishAlert = new PostAlertFinish(activity, aalert.getData() , new ApiResponseListener() { | |
@Override | |
public void success(Object value) { | |
Log.d(activity.getString(R.string.a), activity.getString(R.string.alert_sync_ok) + alert.getID()); | |
//Finalizado | |
} | |
@Override | |
public void error(ApiException e) { | |
stopRunning(); | |
Toast.makeText(activity, e.getErrorMessage(), Toast.LENGTH_LONG).show(); | |
} | |
}); | |
finishAlert.execute(); | |
} | |
private void stopRunning() { | |
isRunning = false; | |
if (this.dialog.isShowing()) | |
this.dialog.dismiss(); | |
setChanged(); | |
notifyObservers(this); | |
} | |
protected void onPreExecute() { | |
this.isRunning = true; | |
this.dialog = new ProgressDialog(activity); | |
this.dialog.setMessage("Enviando datos"); | |
this.dialog.show(); | |
} | |
public Activity getActivity() { | |
return activity; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment