mirror of https://github.com/ThmmyNoLife/mTHMMY
Thodoris1999
6 years ago
4 changed files with 211 additions and 56 deletions
@ -0,0 +1,80 @@ |
|||||
|
package gr.thmmy.mthmmy.utils.parsing; |
||||
|
|
||||
|
import android.os.AsyncTask; |
||||
|
|
||||
|
public abstract class ExternalAsyncTask<U, V> extends AsyncTask<U, Void, V> { |
||||
|
|
||||
|
private OnParseTaskStartedListener onParseTaskStartedListener; |
||||
|
private OnParseTaskCancelledListener onParseTaskCancelledListener; |
||||
|
private OnParseTaskFinishedListener<V> onParseTaskFinishedListener; |
||||
|
|
||||
|
@Override |
||||
|
protected void onPreExecute() { |
||||
|
if (onParseTaskStartedListener != null) |
||||
|
onParseTaskStartedListener.onParseStart(); |
||||
|
else |
||||
|
super.onPreExecute(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void onCancelled() { |
||||
|
if (onParseTaskCancelledListener != null) |
||||
|
onParseTaskCancelledListener.onParseCancel(); |
||||
|
else |
||||
|
super.onCancelled(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void onCancelled(V v) { |
||||
|
if (onParseTaskCancelledListener != null) |
||||
|
onParseTaskCancelledListener.onParseCancel(); |
||||
|
else |
||||
|
super.onCancelled(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void onPostExecute(V v) { |
||||
|
if (onParseTaskFinishedListener != null) |
||||
|
onParseTaskFinishedListener.onParseFinish(v); |
||||
|
else |
||||
|
super.onPostExecute(v); |
||||
|
} |
||||
|
|
||||
|
public ExternalAsyncTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, |
||||
|
OnParseTaskFinishedListener<V> onParseTaskFinishedListener) { |
||||
|
this.onParseTaskStartedListener = onParseTaskStartedListener; |
||||
|
this.onParseTaskCancelledListener = onParseTaskCancelledListener; |
||||
|
this.onParseTaskFinishedListener = onParseTaskFinishedListener; |
||||
|
} |
||||
|
|
||||
|
public ExternalAsyncTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<V> onParseTaskFinishedListener) { |
||||
|
this.onParseTaskStartedListener = onParseTaskStartedListener; |
||||
|
this.onParseTaskFinishedListener = onParseTaskFinishedListener; |
||||
|
} |
||||
|
|
||||
|
public ExternalAsyncTask() { } |
||||
|
|
||||
|
public void setOnParseTaskStartedListener(OnParseTaskStartedListener onParseTaskStartedListener) { |
||||
|
this.onParseTaskStartedListener = onParseTaskStartedListener; |
||||
|
} |
||||
|
|
||||
|
public void setOnParseTaskCancelledListener(OnParseTaskCancelledListener onParseTaskCancelledListener) { |
||||
|
this.onParseTaskCancelledListener = onParseTaskCancelledListener; |
||||
|
} |
||||
|
|
||||
|
public void setOnParseTaskFinishedListener(OnParseTaskFinishedListener<V> onParseTaskFinishedListener) { |
||||
|
this.onParseTaskFinishedListener = onParseTaskFinishedListener; |
||||
|
} |
||||
|
|
||||
|
public interface OnParseTaskStartedListener { |
||||
|
void onParseStart(); |
||||
|
} |
||||
|
|
||||
|
public interface OnParseTaskCancelledListener { |
||||
|
void onParseCancel(); |
||||
|
} |
||||
|
|
||||
|
public interface OnParseTaskFinishedListener<V> { |
||||
|
void onParseFinish(V result); |
||||
|
} |
||||
|
} |
@ -0,0 +1,65 @@ |
|||||
|
package gr.thmmy.mthmmy.utils.parsing; |
||||
|
|
||||
|
import org.jsoup.Jsoup; |
||||
|
import org.jsoup.nodes.Document; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
|
||||
|
import gr.thmmy.mthmmy.base.BaseApplication; |
||||
|
import okhttp3.OkHttpClient; |
||||
|
import okhttp3.Request; |
||||
|
import okhttp3.Response; |
||||
|
import timber.log.Timber; |
||||
|
|
||||
|
public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>> { |
||||
|
|
||||
|
private OnParseTaskFinishedListener<T> onParseTaskFinishedListener; |
||||
|
|
||||
|
@Override |
||||
|
protected final Parcel<T> doInBackground(String... input) { |
||||
|
Request request = createRequest(input); |
||||
|
Response response = getResponse(request, BaseApplication.getInstance().getClient()); |
||||
|
String responseBodyString; |
||||
|
try { |
||||
|
responseBodyString = response.body().string(); |
||||
|
} catch (IOException e) { |
||||
|
Timber.e(e); |
||||
|
return new Parcel<>(Parcel.ResultCode.NETWORK_ERROR, null); |
||||
|
} |
||||
|
try { |
||||
|
T data = performTask(Jsoup.parse(responseBodyString)); |
||||
|
int resultCode = getResultCode(response, data); |
||||
|
return new Parcel<>(resultCode, data); |
||||
|
} catch (ParseException pe) { |
||||
|
Timber.e(pe); |
||||
|
return new Parcel<>(Parcel.ResultCode.PARSE_ERROR, null); |
||||
|
} catch (Exception e) { |
||||
|
Timber.e(e); |
||||
|
return new Parcel<>(Parcel.ResultCode.PERFORM_TASK_ERROR, null); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void onPostExecute(Parcel<T> tParcel) { |
||||
|
if (onParseTaskFinishedListener != null) |
||||
|
onParseTaskFinishedListener.onParseFinish(tParcel.getResultCode(), tParcel.getData()); |
||||
|
else |
||||
|
super.onPostExecute(tParcel); |
||||
|
} |
||||
|
|
||||
|
abstract Request createRequest(String... input); |
||||
|
|
||||
|
abstract Response getResponse(Request request, OkHttpClient client); |
||||
|
|
||||
|
abstract T performTask(Document document) throws ParseException; |
||||
|
|
||||
|
abstract int getResultCode(Response response, T data); |
||||
|
|
||||
|
public void setOnParseTaskFinishedListener(OnParseTaskFinishedListener<T> onParseTaskFinishedListener) { |
||||
|
this.onParseTaskFinishedListener = onParseTaskFinishedListener; |
||||
|
} |
||||
|
|
||||
|
public interface OnParseTaskFinishedListener<T> { |
||||
|
void onParseFinish(int resultCOde, T data); |
||||
|
} |
||||
|
} |
@ -1,68 +1,27 @@ |
|||||
package gr.thmmy.mthmmy.utils.parsing; |
package gr.thmmy.mthmmy.utils.parsing; |
||||
|
|
||||
import android.os.AsyncTask; |
import org.jsoup.nodes.Document; |
||||
|
|
||||
public abstract class NewParseTask<U, V> extends AsyncTask<U, Void, V> { |
import okhttp3.OkHttpClient; |
||||
|
import okhttp3.Request; |
||||
|
import okhttp3.Response; |
||||
|
|
||||
private OnParseTaskStartedListener onParseTaskStartedListener; |
public abstract class NewParseTask<T> extends NetworkTask<T> { |
||||
private OnParseTaskCancelledListener onParseTaskCancelledListener; |
|
||||
private OnParseTaskFinishedListener<V> onParseTaskFinishedListener; |
|
||||
|
|
||||
@Override |
@Override |
||||
protected void onPreExecute() { |
final T performTask(Document document) throws ParseException { |
||||
if (onParseTaskStartedListener != null) |
try { |
||||
onParseTaskStartedListener.onParseStart(); |
return parse(document); |
||||
else |
} catch (Exception e) { |
||||
super.onPreExecute(); |
throw new ParseException("Parse failed."); |
||||
|
} |
||||
} |
} |
||||
|
|
||||
@Override |
abstract Request createRequest(String... input); |
||||
protected void onCancelled() { |
|
||||
if (onParseTaskCancelledListener != null) |
|
||||
onParseTaskCancelledListener.onParseCancel(); |
|
||||
else |
|
||||
super.onCancelled(); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void onCancelled(V v) { |
|
||||
if (onParseTaskCancelledListener != null) |
|
||||
onParseTaskCancelledListener.onParseCancel(); |
|
||||
else |
|
||||
super.onCancelled(); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void onPostExecute(V v) { |
|
||||
if (onParseTaskFinishedListener != null) |
|
||||
onParseTaskFinishedListener.onParseFinish(v); |
|
||||
else |
|
||||
super.onPostExecute(v); |
|
||||
} |
|
||||
|
|
||||
public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, |
abstract Response getResponse(Request request, OkHttpClient client); |
||||
OnParseTaskFinishedListener<V> onParseTaskFinishedListener) { |
|
||||
this.onParseTaskStartedListener = onParseTaskStartedListener; |
|
||||
this.onParseTaskCancelledListener = onParseTaskCancelledListener; |
|
||||
this.onParseTaskFinishedListener = onParseTaskFinishedListener; |
|
||||
} |
|
||||
|
|
||||
public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<V> onParseTaskFinishedListener) { |
|
||||
this.onParseTaskStartedListener = onParseTaskStartedListener; |
|
||||
this.onParseTaskFinishedListener = onParseTaskFinishedListener; |
|
||||
} |
|
||||
|
|
||||
public NewParseTask() { } |
|
||||
|
|
||||
public interface OnParseTaskStartedListener { |
abstract T parse (Document document); |
||||
void onParseStart(); |
|
||||
} |
|
||||
|
|
||||
public interface OnParseTaskCancelledListener { |
|
||||
void onParseCancel(); |
|
||||
} |
|
||||
|
|
||||
public interface OnParseTaskFinishedListener<V> { |
abstract int getResultCode(Response response, T data); |
||||
void onParseFinish(V result); |
|
||||
} |
|
||||
} |
} |
||||
|
@ -0,0 +1,51 @@ |
|||||
|
package gr.thmmy.mthmmy.utils.parsing; |
||||
|
|
||||
|
public class Parcel<T> { |
||||
|
|
||||
|
private int resultCode; |
||||
|
private T data; |
||||
|
|
||||
|
public Parcel(int resultCode, T data) { |
||||
|
this.resultCode = resultCode; |
||||
|
this.data = data; |
||||
|
} |
||||
|
|
||||
|
public int getResultCode() { |
||||
|
return resultCode; |
||||
|
} |
||||
|
|
||||
|
public T getData() { |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
public class ResultCode { |
||||
|
/** |
||||
|
* The request was successful |
||||
|
*/ |
||||
|
public static final int SUCCESSFUL = 0; |
||||
|
/** |
||||
|
* Error 404, page was not found |
||||
|
*/ |
||||
|
public static final int NOT_FOUND = 1; |
||||
|
/** |
||||
|
* User session ended while posting the reply |
||||
|
*/ |
||||
|
public static final int SESSION_ENDED = 2; |
||||
|
/** |
||||
|
* Exception occured while parsing |
||||
|
*/ |
||||
|
public static final int PARSE_ERROR = 3; |
||||
|
/** |
||||
|
* Other undefined of unidentified error |
||||
|
*/ |
||||
|
public static final int OTHER_ERROR = 4; |
||||
|
/** |
||||
|
* Failed to connect to thmmy.gr |
||||
|
*/ |
||||
|
public static final int NETWORK_ERROR = 5; |
||||
|
/** |
||||
|
* Error while excecuting NetworkTask's performTask() |
||||
|
*/ |
||||
|
public static final int PERFORM_TASK_ERROR = 6; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue