Search in sources :

Example 1 with JsonHttpResponseHandler

use of com.loopj.android.http.JsonHttpResponseHandler in project AnimeTaste by daimajia.

the class LoadActivity method init.

private void init() {
    if (getIntent().getAction().equals(Intent.ACTION_VIEW)) {
        Uri uri = getIntent().getData();
        if (uri == null) {
            error();
        }
        String vid = uri.getQueryParameter("vid");
        int animationId = Integer.valueOf(vid);
        ApiConnector.instance().getDetail(animationId, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, final JSONObject response) {
                super.onSuccess(statusCode, response);
                try {
                    if (statusCode == 200 && response.has("data") && response.getJSONObject("data").has("result") && response.getJSONObject("data").getBoolean("result")) {
                        final JSONObject anime = response.getJSONObject("data").getJSONObject("anime");
                        new Thread() {

                            @Override
                            public void run() {
                                super.run();
                                MobclickAgent.onEvent(mContext, "yell");
                                final Intent intent = new Intent(mContext, PlayActivity.class);
                                Animation animation = Animation.build(anime);
                                intent.putExtra("Animation", animation);
                                runOnUiThread(new Runnable() {

                                    @Override
                                    public void run() {
                                        startActivity(intent);
                                        finish();
                                    }
                                });
                            }
                        }.start();
                    } else {
                        error();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    error();
                }
            }

            @Override
            public void onFailure(Throwable throwable, JSONArray jsonArray) {
                super.onFailure(throwable, jsonArray);
                error();
            }

            @Override
            public void onFinish() {
                super.onFinish();
            }
        });
    } else {
        ApiConnector.instance().getInitData(20, 5, 2, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, final JSONObject response) {
                super.onSuccess(response);
                if (statusCode == 200 && response.has("data")) {
                    Message msg = Message.obtain();
                    msg.obj = response;
                    executeHandler.sendMessage(msg);
                } else {
                    error();
                }
            }

            @Override
            public void onFailure(Throwable error) {
                super.onFailure(error);
                error();
            }
        });
    }
}
Also used : Message(android.os.Message) JSONArray(org.json.JSONArray) Intent(android.content.Intent) Uri(android.net.Uri) JSONObject(org.json.JSONObject) Animation(com.zhan_dui.model.Animation) JsonHttpResponseHandler(com.loopj.android.http.JsonHttpResponseHandler)

Example 2 with JsonHttpResponseHandler

use of com.loopj.android.http.JsonHttpResponseHandler in project app by TourLive.

the class APIClient method getRiders.

public static String getRiders(String url, RequestParams params) {
    final String[] messages = { "success" };
    APIClient.get(url, null, new JsonHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject data) {
            try {
                Parser.parseRidersAndPersist(data.getJSONArray("data"));
                messages[0] = "success";
            } catch (Exception ex) {
                messages[0] = ex.getMessage();
            }
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray riders) {
        // Not needed and therefore not implemented
        }

        @Override
        public void onFailure(int error, Header[] headers, Throwable throwable, JSONObject riders) {
            if (throwable.getMessage().equals(throwableType)) {
                messages[0] = readTimeOutMessage + throwable.getMessage();
            } else {
                messages[0] = throwable.getMessage();
            }
        }
    });
    return messages[0];
}
Also used : Header(cz.msebera.android.httpclient.Header) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JsonHttpResponseHandler(com.loopj.android.http.JsonHttpResponseHandler) JSONException(org.json.JSONException)

Example 3 with JsonHttpResponseHandler

use of com.loopj.android.http.JsonHttpResponseHandler in project app by TourLive.

the class APIClient method getRewards.

public static String getRewards(String url, RequestParams params) {
    final String[] messages = { "success" };
    APIClient.get(url, null, new JsonHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject data) {
            try {
                Parser.parseRewardsAndPersist(data.getJSONObject("data").getJSONArray("rewards"));
                messages[0] = "success";
            } catch (Exception ex) {
                messages[0] = ex.getMessage();
            }
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray riders) {
        // Not needed and therefore not implemented
        }

        @Override
        public void onFailure(int error, Header[] headers, Throwable throwable, JSONObject riders) {
            if (throwable.getMessage().equals(throwableType)) {
                messages[0] = readTimeOutMessage + throwable.getMessage();
            } else {
                messages[0] = throwable.getMessage();
            }
        }
    });
    return messages[0];
}
Also used : Header(cz.msebera.android.httpclient.Header) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JsonHttpResponseHandler(com.loopj.android.http.JsonHttpResponseHandler) JSONException(org.json.JSONException)

Example 4 with JsonHttpResponseHandler

use of com.loopj.android.http.JsonHttpResponseHandler in project app by TourLive.

the class APIClient method getMaillotsRiderConnections.

public static String getMaillotsRiderConnections(String url, RequestParams params) {
    final String[] messages = { "success" };
    APIClient.get(url, null, new JsonHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject data) {
            try {
                Parser.parseMaillotsRiderConnectionAndPersist(data.getJSONArray("data"));
                messages[0] = "success";
            } catch (Exception ex) {
                messages[0] = ex.getMessage();
            }
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray maillots) {
        // Not needed and therefore not implemented
        }

        @Override
        public void onFailure(int error, Header[] headers, Throwable throwable, JSONObject riders) {
            if (throwable.getMessage().equals(throwableType)) {
                messages[0] = readTimeOutMessage + throwable.getMessage();
            } else {
                messages[0] = throwable.getMessage();
            }
        }
    });
    return messages[0];
}
Also used : Header(cz.msebera.android.httpclient.Header) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JsonHttpResponseHandler(com.loopj.android.http.JsonHttpResponseHandler) JSONException(org.json.JSONException)

Example 5 with JsonHttpResponseHandler

use of com.loopj.android.http.JsonHttpResponseHandler in project app by TourLive.

the class APIClient method getStateFromAPI.

public static <T> T getStateFromAPI(String url, RequestParams params) {
    if (Looper.myLooper() == null)
        Looper.prepare();
    uiHandler = new Handler();
    T[] response = (T[]) new Object[1];
    APIClient.get(url, null, new JsonHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject data) {
            try {
                response[0] = (T) data;
            } catch (Exception ex) {
                Log.d("here", "here");
                uiHandler.post(ex::getMessage);
            }
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray data) {
            try {
                response[0] = (T) data;
            } catch (Exception ex) {
                Log.d("here", "here");
                uiHandler.post(ex::getMessage);
            }
        }

        @Override
        public void onFailure(int error, Header[] headers, Throwable throwable, JSONObject data) {
            Log.d("here", "here");
            uiHandler.post(throwable::getMessage);
        }
    });
    return response[0];
}
Also used : JSONArray(org.json.JSONArray) JsonHttpResponseHandler(com.loopj.android.http.JsonHttpResponseHandler) Handler(android.os.Handler) AsyncHttpResponseHandler(com.loopj.android.http.AsyncHttpResponseHandler) JSONException(org.json.JSONException) Header(cz.msebera.android.httpclient.Header) JSONObject(org.json.JSONObject) JsonHttpResponseHandler(com.loopj.android.http.JsonHttpResponseHandler)

Aggregations

JsonHttpResponseHandler (com.loopj.android.http.JsonHttpResponseHandler)10 JSONArray (org.json.JSONArray)10 JSONObject (org.json.JSONObject)10 Header (cz.msebera.android.httpclient.Header)9 JSONException (org.json.JSONException)9 Handler (android.os.Handler)2 AsyncHttpResponseHandler (com.loopj.android.http.AsyncHttpResponseHandler)2 Intent (android.content.Intent)1 Uri (android.net.Uri)1 Message (android.os.Message)1 Animation (com.zhan_dui.model.Animation)1