1 package de.dhbwloe.campusapp.network;
3 import android.util.Log;
5 import com.loopj.android.http.AsyncHttpClient;
6 import com.loopj.android.http.AsyncHttpResponseHandler;
8 import org.json.JSONArray;
9 import org.json.JSONException;
10 import org.json.JSONObject;
12 import java.util.List;
14 import cz.msebera.android.httpclient.Header;
17 * Created by pk910 on 09.03.2016.
19 public abstract class JsonRequestHelper {
21 protected void requestJsonFromWeb(String url) {
22 AsyncHttpClient client = new AsyncHttpClient();
24 Log.i("HTTPClient", "Request: " + url);
25 client.get(url, new AsyncHttpResponseHandler() {
27 public void onStart() {
31 public void onSuccess(int statusCode, Header[] headers, byte[] response) {
32 String recvStr = new String(response);
33 JSONObject jsonObj = null;
34 JSONArray jsonArr = null;
36 jsonObj = new JSONObject(recvStr);
37 } catch (JSONException e) {
41 jsonArr = new JSONArray(recvStr);
42 } catch (JSONException e) {
46 onJsonReceived(jsonObj);
47 else if(jsonArr != null)
48 onJsonReceived(jsonArr);
50 onTextReceived(recvStr);
54 public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
57 error = new String(errorResponse, "US-ASCII");
58 } catch (Exception e1) {
60 Log.i("HTTPClient", " Error: " + statusCode + " - " + error);
61 onJsonRequestFail(statusCode, error);
65 public void onRetry(int retryNo) {
70 protected abstract void onJsonReceived(JSONObject json);
71 protected abstract void onJsonReceived(JSONArray json);
72 protected abstract void onTextReceived(String response);
73 protected abstract void onJsonRequestFail(int statusCode, String errorMessage);