GimiCinemaBusiness.java 3.74 KB
package com.gimi.common.cinema.utils;

import android.content.Context;
import android.text.TextUtils;
import com.gimi.common.cinema.model.AsyncCallback;
import com.gimi.common.cinema.model.MovieMessage;
import com.gimi.common.cinema.model.Urls;
import com.gimi.common.cinema.model.WrongMsg;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by pc on 2014/12/23.
 */
public class GimiCinemaBusiness {
    private Context context;


    public GimiCinemaBusiness(Context context) {
        this.context = context;
    }

    public void getMovieMessage(final String id, final AsyncCallback<MovieMessage> callback) {
        new Thread() {
            public void run() {
                if (callback != null)
                    callback.onStart();
                WrongMsg wrongMsg = new WrongMsg();
                MovieMessage result = null;
                try {
                    Gson gson = new Gson();
                    String s = "";
                    s = OkHttpClientManager.getAsString(Urls.BASIC_URL + Urls.MOVIE_MESSAGE + id.trim());
                    wrongMsg = gson.fromJson(s, new TypeToken<WrongMsg>() {
                    }.getType());
                    if (wrongMsg.getCode() != 0) {
                        if (callback != null)
                            callback.onFailure(wrongMsg);
                    } else {
                        result = gson.fromJson(s, new TypeToken<MovieMessage>() {
                        }.getType());
                        if (callback != null)
                            callback.onSuccess(result);
                    }
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (callback != null)
                    callback.onDone();
            }
        }.start();
    }

    public Thread getMovieMessages(final String id, final AsyncCallback<MovieMessage> callback) {
        return new Thread() {
            public void run() {
                if (callback != null)
                    callback.onStart();
                WrongMsg wrongMsg = new WrongMsg();
                MovieMessage result = null;
                Gson gson = new Gson();
                String s = "";
                List<String> keys = new ArrayList<String>();
                List<String> values = new ArrayList<String>();
                try {
                    s = OkHttpClientManager.getAsString(Urls.BASIC_URL + Urls.MOVIE_MESSAGE + id);
                } catch (IOException e) {
                    e.printStackTrace();
                    if (callback != null) {
                        wrongMsg.setMsg(e.getMessage());
                        callback.onFailure(wrongMsg);
                    }
                }
                if (TextUtils.isEmpty(s)) {
                    if (callback != null) {
                        wrongMsg.setMsg("json msg is empty");
                        callback.onFailure(wrongMsg);
                    }
                }
                wrongMsg = gson.fromJson(s, new TypeToken<WrongMsg>() {
                }.getType());
                if (wrongMsg.getCode() != 0) {
                    if (callback != null)
                        callback.onFailure(wrongMsg);
                } else {
                    result = gson.fromJson(s, new TypeToken<MovieMessage>() {
                    }.getType());
                    if (callback != null)
                        callback.onSuccess(result);
                }
                if (callback != null)
                    callback.onDone();
            }
        };
    }

}