3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 package de.dhbwloe.campusapp.search;
18 import android.util.Log;
20 import com.loopj.android.http.AsyncHttpResponseHandler;
22 import org.jsoup.Jsoup;
23 import org.jsoup.nodes.Document;
25 import java.io.UnsupportedEncodingException;
26 import java.util.List;
28 import cz.msebera.android.httpclient.Header;
29 import de.dhbwloe.campusapp.fragments.AppSearchListItem;
32 * Created by pk910 on 25.01.2016.
34 public abstract class SearchHelper {
36 protected boolean bRequestRunning = false;
37 protected boolean bHtmlParser;
38 protected SearchResultListener oRequestListener = null;
40 public SearchHelper(boolean htmlparser) {
41 bHtmlParser = htmlparser;
44 protected void performWebRequest(String keywords) {
45 AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler() {
47 public void onStart() {
50 public void onSuccess(int statusCode, Header[] headers, byte[] response) {
51 boolean parsedHttp = false;
54 Document document = Jsoup.parse(new String(response));
57 bRequestRunning = false;
59 List<AppSearchListItem> results = onHtmlDocumentReceived(document);
60 oRequestListener.onSearchResultsReceived(results);
61 } catch(Exception e) {
62 oRequestListener.onSearchFailed(e.getMessage());
64 } catch(Exception e) {
69 List<AppSearchListItem> results = onPlainTextReceived(response);
70 oRequestListener.onSearchResultsReceived(results);
71 } catch(Exception e) {
72 oRequestListener.onSearchFailed(e.getMessage());
75 bRequestRunning = false;
78 public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
81 error = new String(errorResponse, "US-ASCII");
82 } catch (Exception e1) {
84 Log.i("HTTPClient", " Error: " + statusCode + " - " + error);
85 bRequestRunning = false;
86 oRequestListener.onSearchFailed(statusCode + ": " + error);
89 public void onRetry(int retryNo) {
92 processWebRequest(handler, keywords);
95 public void search(String keywords, SearchResultListener resultlistener) {
97 resultlistener.onSearchFailed("another request is already running!");
100 oRequestListener = resultlistener;
101 bRequestRunning = true;
103 performWebRequest(keywords);
106 protected abstract void processWebRequest(AsyncHttpResponseHandler handler, String keywords);
107 protected abstract List<AppSearchListItem> onHtmlDocumentReceived(Document document);
108 protected abstract List<AppSearchListItem> onPlainTextReceived(byte[] data);