Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStreamReader;

import android.content.Context;

import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

public class Main {
    private static final String TAG = "TaxiUtil";
    static final String TAXICOOKIE_FILE = "taxi_cookie";

    public static void setCookie(Context context, String url) {
        FileInputStream in = null;
        try {
            in = context.openFileInput(TAXICOOKIE_FILE);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (in == null) {
            Log.w(TAG, "saveCookie: Cannot open file: " + TAXICOOKIE_FILE);
        }

        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String cookieStr = null;
        try {
            cookieStr = reader.readLine();
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Log.d(TAG, "cookieStr: " + cookieStr);
        if (cookieStr == null) {
            return;
        }

        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeSessionCookie();
        cookieManager.setCookie(url, cookieStr);
        CookieSyncManager.getInstance().sync();
    }
}