ZConnect

基於OkHttp的客製化封裝連線。(可直接在response處更新UI)

支援的上傳檔案類型

@uploadFileType
_FILE_TYPE_JPG = "image/jpg"
_FILE_TYPE_PNG = "image/png"
_FILE_TYPE_CSV = "text/csv"

---

連線CallBack

public class ZConnectHandler {
    //-----------

    /***
     * 適用inputstream接口
     * inputStream 回傳流
     * code 狀態碼
     */
    public void OnInputStreamResponse(InputStream inputStream,int code) 


    //-----------

    /***
     * 適用 json String 接口
     * response 回傳字串
     * code 狀態碼
     */
    public void OnStringResponse(String response,int code) 


   //-----------

    /***
     *  連線失敗接口
     */
    public void OnFail(IOException e) 
    
    //----------

    /***
     *  連線成功但回覆失敗接口
     */
    public void OnFail(String response, int code) 

}

---

設置


/***
* 自訂客製化dialog
* 如不欲顯示LoadingDialog 將參數設為null即可
* 不使用此方法則會以預設Loading執行
*/
public void setCustomLoadingDialog(@Nullable Dialog dialog)

//------------------

/***
* 預設 15秒 不欲修改則不需使用此方法
* connectTimeOut 連線time out 單位毫秒
*/
public void setConnectTimeOut(int connectTimeOut)

//------------------

/***
* 預設 15秒 不欲修改則不需使用此方法
* writeTimeOut 寫入time out 單位毫秒
*/
public void setWriteTimeOut(int writeTimeOut)

//------------------

/***
* 預設 15秒 不欲修改則不需使用此方法
* readTimeOut 讀取time out 單位毫秒
*/
public void setReadTimeOut(int readTimeOut)

---

連線


/***
* 使用情境:
* 1.http get
* ... ... ... ... ... ...
* apiURL url
* zConnectCallback callback
*/
public void get(String apiURL, ZConnectHandler zConnectHandler) 

		Sample :

        zConnect.get("your url", new ZConnectHandler() {
            
            /***
             *  連線成功但回覆失敗接口
             */
            public void OnFail(String response, int code) {
               super.OnFail(response,code);

            }
            /**
            *  onFail
            */
            @Override
            public void OnFail(IOException e) {
                super.OnFail(e);
            }

            /***
            *  當回傳串流
            * @param inputStream
            * @param code 狀態碼
            */
            @Override
            public void OnInputStreamResponse(InputStream inputStream, int code) {
                super.OnInputStreamResponse(inputStream, code);
            }

            /***
            *  當回傳字串
            * @param response
            * @param code 狀態碼
            */
            @Override
            public void OnStringResponse(String response, int code) {
                super.OnStringResponse(response, code);
            }
        });

//------------------

/***
* 使用情境:
* 1.http get
* 2.headers 
* ... ... ... ... ... ...
* apiURL url
* headers header 
* zConnectCallback callback
*/
public void get(String apiURL, Map<String, String> headers, ZConnectHandler zConnectHandler) 

		Sample :

        Map<String,String> headers = new HashMap<>();
        headers.put("token","AABBCCDDEEFFGGHHIIJJKKLL");

        zConnect.get("your url",headers, new ZConnectHandler() {
            
            /***
             *  連線成功但回覆失敗接口
             */
            public void OnFail(String response, int code) {
               super.OnFail(response,code);
            }
            /**
            *  onFail
            */
            @Override
            public void OnFail(IOException e) {
                super.OnFail(e);
            }
            /**
             * onFail
             */
            @Override
            public void OnFail() {
                super.OnFail();
            }

            /***
             * 當回傳串流
             * @param inputStream
             * @param code 狀態碼
             */
            @Override
            public void OnInputStreamResponse(InputStream inputStream, int code) {
                super.OnInputStreamResponse(inputStream, code);
            }

            /***
             * 當回傳字串
             * @param response
             * @param code 狀態碼
             */
            @Override
            public void OnStringResponse(String response, int code) {
                super.OnStringResponse(response, code);
            }
        });
    

//------------------

/***
* 使用情境:
* 1.http post
* 2.headers 
* 3.params
* ... ... ... ... ... ...
* apiURL url
* headers header 用不到則帶null即可
* params keyValue 參數
* zConnectCallback callback
*/
public void post(String apiURL, @Nullable Map<String, String> headers, @Nullable Map<String, String> params, ZConnectHandler zConnectHandler) {

		Sample :

        Map<String, String> headers = new HashMap<>();
        headers.put("token", "AABBCCDDEEFFGGHHIIJJKKLL");

        Map<String, String> params = new HashMap<>();
        params.put("name", "noel");
        params.put("age", "26");
        params.put("sex", "male");
        
        zConnect.post("your url", headers, params, new ZConnectHandler() {
            /***
             *  連線成功但回覆失敗接口
             */
            public void OnFail(String response, int code) {
               super.OnFail(response,code);
            }
            /**
            *  onFail
            */
            @Override
            public void OnFail(IOException e) {
                super.OnFail(e);
            }

            /***
             *  當回傳串流
             * @param inputStream
             * @param code 狀態碼
             */
            @Override
            public void OnInputStreamResponse(InputStream inputStream, int code) {
                super.OnInputStreamResponse(inputStream, code);
            }

            /***
             *  當回傳字串
             * @param response
             * @param code 狀態碼
             */
            @Override
            public void OnStringResponse(String response, int code) {
                super.OnStringResponse(response, code);
            }
        });

//------------------

/***
* 使用情境:
* 1.http post
* 2.headers 
* 3.json request
* ... ... ... ... ... ...
* apiURL url
* headers header 用不到則帶null即可
* requestModel  json object model  用不到則帶null即可
* zConnectCallback callback
*/
public void post(String apiURL, @Nullable Map<String, String> headers, Object requestModel, ZConnectHandler zConnectHandler)

		Sample:

		public class PersonModel(){
			private String name;
			private String age;
			private String sex;
			
			public PersonModel(String name,String age,String sex){
				this.name = name;
				this.age = age;
				this.sex = sex;
			}
		}

		//------

        zConnect.post("your url", headers, params,new PersonModel("noel","26","male") ,new ZConnectHandler() {
            /***
             *  連線成功但回覆失敗接口
             */
            public void OnFail(String response, int code) {
               super.OnFail(response,code);
            }
            /**
            *  onFail
            */
            @Override
            public void OnFail(IOException e) {
                super.OnFail(e);
            }

            /***
             *  當回傳串流
             * @param inputStream
             * @param code 狀態碼
             */
            @Override
            public void OnInputStreamResponse(InputStream inputStream, int code) {
                super.OnInputStreamResponse(inputStream, code);
            }

            /***
             *  當回傳字串
             * @param response
             * @param code 狀態碼
             */
            @Override
            public void OnStringResponse(String response, int code) {
                super.OnStringResponse(response, code);
            }
        });

//------------------

/***
* 使用情境:
* 1.http post
* 2.上傳檔案
* 3.header
* 4.params
* ... ... ... ... ... ...
* apiURL url
* headers headers 用不到則帶null即可
* params params 用不到則帶null即可
* fileKey 後台此欄位key值
* fileName 檔名
* file 檔案
* fileType 檔案類型 參考 @uploadFileType
* zConnectHandler callback
*/
public void post(String apiURL, @Nullable Map<String, String> headers, @Nullable Map<String, String> params, String fileKey, String fileName, File file, @uploadFileType String fileType, ZConnectHandler zConnectHandler)

		Sample:

        Map<String, String> headers = new HashMap<>();
        headers.put("token", "AABBCCDDEEFFGGHHIIJJKKLL");

        Map<String, String> params = new HashMap<>();
        params.put("name", "noel");
        params.put("age", "26");
        params.put("sex", "male");
        

        zConnect.post("your url", headers, params,"file","fileName.jpg",new File("path"),_FILE_TYPE_JPG, new ZConnectHandler() {
            /***
             *  連線成功但回覆失敗接口
             */
            public void OnFail(String response, int code) {
               super.OnFail(response,code);
            }
            /**
            *  onFail
            */
            @Override
            public void OnFail(IOException e) {
                super.OnFail(e);
            }

            /***
             *  當回傳串流
             * @param inputStream
             * @param code 狀態碼
             */
            @Override
            public void OnInputStreamResponse(InputStream inputStream, int code) {
                super.OnInputStreamResponse(inputStream, code);
            }

            /***
             *  當回傳字串
             * @param response
             * @param code 狀態碼
             */
            @Override
            public void OnStringResponse(String response, int code) {
                super.OnStringResponse(response, code);
            }
        });


//------------------

/***
使用情境:
* 1.http post
* 2.上傳檔案
* 3.header
* 4.json request
* .. ... ... ... ... ...
* apiURL           url
* headers          header 用不到則帶null即可
* requestModel     requestModel 用不到則帶null即可
* fileKey          後台此欄位key值
* fileName         檔名
* file             檔案
* fileType         檔案類型 參考 @uploadFileType
* zConnectHandler callback
*/
public void post(String apiURL, @Nullable Map<String, String> headers, @Nullable Object requestModel, String fileKey, String fileName, File file, @uploadFileType String fileType, ZConnectHandler zConnectHandler)

		Sample:


		public class PersonModel(){
			private String name;
			private String age;
			private String sex;
			
			public PersonModel(String name,String age,String sex){
				this.name = name;
				this.age = age;
				this.sex = sex;
			}
		}

        Map<String, String> headers = new HashMap<>();
        headers.put("token", "AABBCCDDEEFFGGHHIIJJKKLL");



        zConnect.post("your url", headers, new PersonModel("noel","26","male"),"file","fileName.jpg",new File("path"),_FILE_TYPE_JPG, new ZConnectHandler() {
            /***
             *  連線成功但回覆失敗接口
             */
            public void OnFail(String response, int code) {
               super.OnFail(response,code);
            }
            /**
            *  onFail
            */
            @Override
            public void OnFail(IOException e) {
                super.OnFail(e);
            }

            /***
             *  當回傳串流
             * @param inputStream
             * @param code 狀態碼
             */
            @Override
            public void OnInputStreamResponse(InputStream inputStream, int code) {
                super.OnInputStreamResponse(inputStream, code);
            }

            /***
             *  當回傳字串
             * @param response
             * @param code 狀態碼
             */
            @Override
            public void OnStringResponse(String response, int code) {
                super.OnStringResponse(response, code);
            }
        });

Last updated