Created
September 26, 2017 00:53
-
-
Save mgilangjanuar/4ffb6960d790865ee9b917adb0a78b50 to your computer and use it in GitHub Desktop.
Android class for scraping
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
import android.os.AsyncTask; | |
import android.text.TextUtils; | |
import org.jsoup.Connection; | |
import org.jsoup.Jsoup; | |
import org.jsoup.select.Elements; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
/** | |
* Created by mgilangjanuar (mgilangjanuar@gmail.com) | |
* | |
* @since 2017 | |
*/ | |
public abstract class BaseProvider extends AsyncTask<String, Integer, List<Elements>> { | |
protected String url; | |
protected String[] data; | |
protected Connection.Method method; | |
protected Map<String, String> cookies; | |
public BaseProvider() { | |
super(); | |
data = new String[0]; | |
method = Connection.Method.GET; | |
cookies = new HashMap<>(); | |
} | |
public abstract void run(); | |
@Override | |
@Deprecated | |
protected List<Elements> doInBackground(String... params) { | |
List<Elements> results = new ArrayList<>(); | |
int idx = 0; | |
try { | |
Connection.Response response = Jsoup.connect(url) | |
.data(data) | |
.method(method) | |
.cookies(cookies) | |
.execute(); | |
cookies = response.cookies(); | |
for (String param : params) { | |
if (!TextUtils.isEmpty(param)) { | |
results.add(response.parse().select(param)); | |
} | |
publishProgress((int) ((double) (++idx / params.length)) * 100); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return results; | |
} | |
@Override | |
@Deprecated | |
protected void onProgressUpdate(Integer... values) { | |
super.onProgressUpdate(values); | |
onProgressUpdate(values[0]); | |
} | |
private void onProgressUpdate(Integer value) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment