1. 程式人生 > >Okhttp -定義常量和初始化物件

Okhttp -定義常量和初始化物件

   //建立佇列,用於非同步呼叫
    private Request request;

    //執行緒阻塞方式呼叫
    private Response response;

    //訪問物件
    private Call call;

    //okhttp例項
    private OkHttpClient mOkHttpClient;

    //執行緒阻塞方式請求的get,post狀態碼
    private final static int GET_INSTANCE = 0;
    private final static int POST_INSTANCE = 1;

    //預設超時時間
    private final static int VALUE_DEFAULT_TIME_OUT = 20 * 1000;

    /**
     * 引數型別
     * "text", 文字
     * "image", 圖片
     * "audio",音訊
     * "video",視訊
     * "object",其他
     */
    private static final MediaType MEDIA_TYPE_TEXT = MediaType.parse("text/x-markdown; charset=utf-8");
    private static final MediaType MEDIA_TYPE_JPG = MediaType.parse("image/png");
    private static final MediaType MEDIA_TYPE_AUDIO = MediaType.parse("audio/mp3");
    private static final MediaType MEDIA_TYPE_VIDEO = MediaType.parse("video/mp4");
    private static final MediaType MEDIA_TYPE_OBJECT = MediaType.parse("application/octet-stream");
    private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

//給每個網路請求的標記
    public static final String TAG_DATA = "tag_data";
    public static final String TAG_IMAGE = "tag_image";
    public static final String TAG_FILE = "tag_file";

private RequestHandle() {
        //建立okHttpClient物件
        mOkHttpClient = new OkHttpClient();
        mOkHttpClient.setConnectTimeout(VALUE_DEFAULT_TIME_OUT, TimeUnit.MILLISECONDS);//連線超時
        mOkHttpClient.setReadTimeout(VALUE_DEFAULT_TIME_OUT, TimeUnit.MILLISECONDS);//讀取超時
        mOkHttpClient.setWriteTimeout(VALUE_DEFAULT_TIME_OUT, TimeUnit.MILLISECONDS);//寫入超時
    }