Browse Source

Generic ParseTask init

Testing in RecentFragment only at the moment
pull/24/head
Ezerous 8 years ago
parent
commit
fe107b82c1
  1. 2
      app/build.gradle
  2. 71
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  3. 1
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java
  4. 10
      app/src/main/java/gr/thmmy/mthmmy/utils/CrashReportingTree.java
  5. 48
      app/src/main/java/gr/thmmy/mthmmy/utils/ParseTask.java

2
app/build.gradle

@ -33,7 +33,7 @@ dependencies {
compile 'com.android.support:support-v4:25.3.1' compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1' compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1' compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.google.firebase:firebase-crash:11.0.0' compile 'com.google.firebase:firebase-crash:10.2.6'
compile 'com.squareup.okhttp3:okhttp:3.8.0' compile 'com.squareup.okhttp3:okhttp:3.8.0'
compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0' compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'

71
app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java

@ -12,11 +12,9 @@ import android.widget.ProgressBar;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.Toast; import android.widget.Toast;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -27,14 +25,13 @@ import gr.thmmy.mthmmy.base.BaseFragment;
import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.model.TopicSummary;
import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.CustomRecyclerView; import gr.thmmy.mthmmy.utils.CustomRecyclerView;
import gr.thmmy.mthmmy.utils.ParseTask;
import gr.thmmy.mthmmy.utils.exceptions.ParseException; import gr.thmmy.mthmmy.utils.exceptions.ParseException;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import okhttp3.HttpUrl;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response;
import timber.log.Timber; import timber.log.Timber;
/** /**
* A {@link BaseFragment} subclass. * A {@link BaseFragment} subclass.
* Activities that contain this fragment must implement the * Activities that contain this fragment must implement the
@ -84,7 +81,7 @@ public class RecentFragment extends BaseFragment {
if (topicSummaries.isEmpty()) if (topicSummaries.isEmpty())
{ {
recentTask =new RecentTask(); recentTask =new RecentTask();
recentTask.execute(); recentTask.execute(SessionManager.indexUrl.toString());
} }
Timber.d("onActivityCreated"); Timber.d("onActivityCreated");
@ -117,7 +114,7 @@ public class RecentFragment extends BaseFragment {
public void onRefresh() { public void onRefresh() {
if (recentTask != null && recentTask.getStatus() != AsyncTask.Status.RUNNING) { if (recentTask != null && recentTask.getStatus() != AsyncTask.Status.RUNNING) {
recentTask = new RecentTask(); recentTask = new RecentTask();
recentTask.execute(); recentTask.execute(SessionManager.indexUrl.toString());
} }
} }
@ -141,51 +138,21 @@ public class RecentFragment extends BaseFragment {
} }
//---------------------------------------ASYNC TASK----------------------------------- //---------------------------------------ASYNC TASK-----------------------------------
private class RecentTask extends ParseTask {
private class RecentTask extends AsyncTask<Void, Void, Integer> {
private final HttpUrl thmmyUrl = SessionManager.indexUrl;
private Document document;
protected void onPreExecute() { protected void onPreExecute() {
progressBar.setVisibility(ProgressBar.VISIBLE); progressBar.setVisibility(ProgressBar.VISIBLE);
} }
protected Integer doInBackground(Void... voids) { @Override
Request request = new Request.Builder() protected Request prepareRequest(String... strings) {
.url(thmmyUrl) return new Request.Builder()
.url(strings[0])
.build(); .build();
try {
Response response = client.newCall(request).execute();
document = Jsoup.parse(response.body().string());
parse(document);
return 0;
} catch (ParseException e) {
Timber.e(e, "ParseException");
return 1;
} catch (IOException e) {
Timber.i(e, "Network Error");
return 2;
} catch (Exception e) {
Timber.e(e, "Exception");
return 3;
}
} }
@Override
protected void onPostExecute(Integer result) { public void parse(Document document) throws ParseException
{
if (result == 0)
recentAdapter.notifyDataSetChanged();
else if (result == 2)
Toast.makeText(getActivity(), "Network error", Toast.LENGTH_SHORT).show(); //Fixme, sometimes activity isn't ready
progressBar.setVisibility(ProgressBar.INVISIBLE);
swipeRefreshLayout.setRefreshing(false);
}
private void parse(Document document) throws ParseException {
Elements recent = document.select("#block8 :first-child div"); Elements recent = document.select("#block8 :first-child div");
if (!recent.isEmpty()) { if (!recent.isEmpty()) {
topicSummaries.clear(); topicSummaries.clear();
@ -217,6 +184,20 @@ public class RecentFragment extends BaseFragment {
} }
throw new ParseException("Parsing failed"); throw new ParseException("Parsing failed");
} }
@Override
protected void onPostExecute(ParseTask.ResultCode result) {
if (result == ResultCode.SUCCESS)
recentAdapter.notifyDataSetChanged();
else if (result == ResultCode.NETWORK_ERROR)
Toast.makeText(getActivity(), "Network error", Toast.LENGTH_SHORT).show(); //Fixme, sometimes activity isn't ready
progressBar.setVisibility(ProgressBar.INVISIBLE);
swipeRefreshLayout.setRefreshing(false);
}
} }
} }

1
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java

@ -1,7 +1,6 @@
package gr.thmmy.mthmmy.activities.topic; package gr.thmmy.mthmmy.activities.topic;
import android.graphics.Color; import android.graphics.Color;
import android.util.Log;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;

10
app/src/main/java/gr/thmmy/mthmmy/utils/CrashReportingTree.java

@ -14,16 +14,16 @@ public class CrashReportingTree extends DebugTree {
return; return;
} }
String level; char level;
if (priority == Log.INFO) if (priority == Log.INFO)
level = "I"; level = 'I';
else if (priority == Log.WARN) else if (priority == Log.WARN)
level = "W"; level = 'W';
else if(priority == Log.ERROR) else if(priority == Log.ERROR)
level = "E"; level = 'E';
else else
level = "A"; level = 'A';
FirebaseCrash.log(level + "/" + tag + ": " + message); FirebaseCrash.log(level + "/" + tag + ": " + message);

48
app/src/main/java/gr/thmmy/mthmmy/utils/ParseTask.java

@ -0,0 +1,48 @@
package gr.thmmy.mthmmy.utils;
import android.os.AsyncTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.utils.exceptions.ParseException;
import okhttp3.Request;
import okhttp3.Response;
import timber.log.Timber;
public abstract class ParseTask extends AsyncTask<String, Void, ParseTask.ResultCode> {
protected enum ResultCode {
SUCCESS, PARSING_ERROR, NETWORK_ERROR, OTHER_ERROR
}
@Override
protected ResultCode doInBackground(String... params) {
Request request = prepareRequest(params);
try {
Response response = BaseApplication.getInstance().getClient().newCall(request).execute();
Document document = Jsoup.parse(response.body().string());
parse(document);
return ResultCode.SUCCESS;
} catch (ParseException e) {
Timber.tag(this.getClass().getSimpleName());
Timber.e(e, "Parsing Error");
return ResultCode.PARSING_ERROR;
} catch (IOException e) {
Timber.tag(this.getClass().getSimpleName());
Timber.i(e, "Network Error");
return ResultCode.NETWORK_ERROR;
} catch (Exception e) {
Timber.tag(this.getClass().getSimpleName());
Timber.e(e, "Other Error");
return ResultCode.OTHER_ERROR;
}
}
protected abstract Request prepareRequest(String... params);
protected abstract void parse (Document document) throws ParseException;
}
Loading…
Cancel
Save