<commit_before>package com.duang.easyecard.Activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.Toast;

import com.duang.changeiconcolorwithtext.ChangeColorTab;
import com.duang.easyecard.GlobalData.MyApplication;
import com.duang.easyecard.GlobalData.UrlConstant;
import com.duang.easyecard.Model.UserBasicInformation;
import com.duang.easyecard.R;
import com.duang.easyecard.Util.LogUtil;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import cz.msebera.android.httpclient.Header;

/**
 * MainActivity
 * 应用主界面
 * Created by MrD on 2016/2/4.
 */
public class MainActivity extends BaseActivity implements
        ManagementFragment.StartActivitiesCallback {

    private ViewPager mViewPager;
    private List<Fragment> mTabFragments = new ArrayList<>();
    private ChangeColorTab changeColorTab;

    private AsyncHttpClient httpClient;
    private UserBasicInformation userBasicInformation;
    private List<String> userBasicInformationDataList;
    private String response;
    private int startManageActivityFlag;
    private final String TAG = "MainActivity";
    protected final int CONSTANT_START_NOTHING = 0;
    protected static final int CONSTANT_START_BASIC_INFORMATION = 1;
    protected static final int CONSTANT_START_TRADING_INQUIRY = 2;
    protected static final int CONSTANT_START_REPORT_LOSS = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }

    //FindView
    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.main_view_pager);
        changeColorTab = (ChangeColorTab) findViewById(R.id.main_change_color_tab);
    }

    //初始化所有事件
    private void initEvent() {
        changeColorTab.setViewpager(mViewPager);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        }
    }

    //初始化四个Fragment
    private void initData() {
        // 设置ViewPager和TabLayout
        mTabFragments.add(new ManagementFragment());
        mTabFragments.add(new LostAndFoundFragment());
        mTabFragments.add(new MessagesFragment());
        mTabFragments.add(new SettingsFragment());
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mTabFragments.size();
            }

            @Override
            public Fragment getItem(int position) {
                return mTabFragments.get(position);
            }
        });
        // 获得全局变量httpClient
        MyApplication myApp = (MyApplication) getApplication();
        httpClient = myApp.getHttpClient();
        // 发送GET请求，获取基本信息
        sendGETRequestToMobile(CONSTANT_START_NOTHING);
    }

    // 向“掌上校园”发送GET请求，有时该服务器会存在问题，失效时向校园卡服务平台发送GET请求
    public void sendGETRequestToMobile(int startActivityFlag) {
        startManageActivityFlag = startActivityFlag;
        httpClient.get(UrlConstant.MOBILE_MANAGE_BASIC_INFO, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                // 成功响应
                response = new String(responseBody);
                // 解析response
                new JsoupHtmlDataFromMobile().execute();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody,
                                  Throwable error) {
                // 网络错误
                Toast.makeText(MainActivity.this, R.string.network_error,
                        Toast.LENGTH_SHORT).show();
                error.printStackTrace();
            }
        });
    }

    @Override
    public void sendPrePostRequestForTradingInquiry() {
        // 装填POST数据
        RequestParams params = new RequestParams();
        params.add("needHeader", "false");
        // 发送POST请求
        httpClient.post(UrlConstant.TRJN_QUERY, params, new AsyncHttpResponseHandler() {
            // 成功响应，刷新全局httpClient
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                MyApplication myApp = (MyApplication) getApplication();
                myApp.setHttpClient(httpClient);
                LogUtil.d(TAG, new String(responseBody));<commit_msg>Deal with the situation that the web app crashes.<commit_after>package com.duang.easyecard.Activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.Toast;

import com.duang.changeiconcolorwithtext.ChangeColorTab;
import com.duang.easyecard.GlobalData.MyApplication;
import com.duang.easyecard.GlobalData.UrlConstant;
import com.duang.easyecard.Model.UserBasicInformation;
import com.duang.easyecard.R;
import com.duang.easyecard.Util.LogUtil;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import cz.msebera.android.httpclient.Header;

/**
 * MainActivity
 * 应用主界面
 * Created by MrD on 2016/2/4.
 */
public class MainActivity extends BaseActivity implements
        ManagementFragment.StartActivitiesCallback {

    private ViewPager mViewPager;
    private List<Fragment> mTabFragments = new ArrayList<>();
    private ChangeColorTab changeColorTab;

    private AsyncHttpClient httpClient;
    private UserBasicInformation userBasicInformation;
    private List<String> userBasicInformationDataList;
    private String response;
    private int startManageActivityFlag;
    private final String TAG = "MainActivity";
    protected final int CONSTANT_START_NOTHING = 0;
    protected static final int CONSTANT_START_BASIC_INFORMATION = 1;
    protected static final int CONSTANT_START_TRADING_INQUIRY = 2;
    protected static final int CONSTANT_START_REPORT_LOSS = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }

    //FindView
    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.main_view_pager);
        changeColorTab = (ChangeColorTab) findViewById(R.id.main_change_color_tab);
    }

    //初始化所有事件
    private void initEvent() {
        changeColorTab.setViewpager(mViewPager);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        }
    }

    //初始化四个Fragment
    private void initData() {
        // 设置ViewPager和TabLayout
        mTabFragments.add(new ManagementFragment());
        mTabFragments.add(new LostAndFoundFragment());
        mTabFragments.add(new MessagesFragment());
        mTabFragments.add(new SettingsFragment());
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mTabFragments.size();
            }

            @Override
            public Fragment getItem(int position) {
                return mTabFragments.get(position);
            }
        });
        // 获得全局变量httpClient
        MyApplication myApp = (MyApplication) getApplication();
        httpClient = myApp.getHttpClient();
        // 发送GET请求，获取基本信息
        sendGETRequestToMobile(CONSTANT_START_NOTHING);
    }

    // 向“掌上校园”发送GET请求，有时该服务器会存在问题，失效时向校园卡服务平台发送GET请求
    public void sendGETRequestToMobile(int startActivityFlag) {
        startManageActivityFlag = startActivityFlag;
        // 设置重试次数为1，Timeout时间为1秒。否则移动版出问题时，会等待较长时间
        httpClient.setMaxRetriesAndTimeout(1, 1000);
        httpClient.get(UrlConstant.MOBILE_MANAGE_BASIC_INFO, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                // 成功响应
                response = new String(responseBody);
                // 解析response
                new JsoupHtmlDataFromMobile().execute();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody,
                                  Throwable error) {
                // 网络错误
                LogUtil.d(TAG, "Failed to get data from MOBILE. Now send POST Request to platform");
                // 向服务平台发送POST请求
                sendPOSTRequestToServicePlatform();
                error.printStackTrace();
            }
        });
    }

    @Override
    public void sendPrePostRequestForTradingInquiry() {
        // 装填POST数据
        RequestParams params = new RequestParams();
        params.add("needHeader", "false");
        // 发送POST请求
        httpClient.post(UrlConstant.TRJN_QUERY, params, new AsyncHttpResponseHandler() {
            // 成功响应，刷新全局httpClient
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                MyApplication myApp = (MyApplication) getApplication();
                myApp.setHttpClient(httpClient);
                LogUtil.d(TAG, new String(responseBody));
{'fraction_non_alphanumeric': 0.08697803285177122, 'fraction_numerical': 0.0032653868988719572, 'mean_word_length': 2.840045592705167, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3955618', 'n_tokens_mistral': 3024, 'n_tokens_neox': 2991, 'n_words': 542}