Skip to content

crazyma/JsonCake-old

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Welcome to JsonCake

這是一個Android Library,方便developer開發Android App時,快速執行下載或上傳Json file,讓你使用Json進行資料傳輸有如piece of cake

本Library使用了 OKHttp 以及 Gson 協助處理相關的操作。

Variables

Required

Name Data type Description
urlStr String json file 的網址。
onFinishListener OnFinishListener Task 結束時呼叫的Listener。

Optional

Name Data type Description
connectionTimeout int 連線至server的timeout時間,單位是秒。
readTimeout int Http get 的 timeout時間,單位是秒。
writeTimeout int Http post 的timeout時間,單位是秒。
onTaskFailListener OnTaskFailListener Task 失敗時呼叫的Listener。
formBody RequestBody Http post所使用。是OkHttp所定義的類別。
objectType Type 類別型態。配合Gson使用。

Sample Code

Load JSONObject Object

new JsonCake.Builder()
	.setUrl("your_json_file_url")
    .setOnFinishListener(new OnFinishLoadJsonObjectListener(){

		@Override
		public void onFinish(JSONObject arg0) {
			// TODO Auto-generated method stub
			/* write your code here */
		}
    		
    })
    .get();

Load JSONArray Object with Read Timeout

new JsonCake.Builder()
	.setUrl("your_json_file_url")
    .setReadTimeout(5)
    .setOnFinishListener(new OnFinishLoadJsonArrayListener(){

		@Override
		public void onFinish(JSONArray arg0) {
			// TODO Auto-generated method stub
			/* write your code here */
		}
    		
    })
    .get();

Load String Object with OnTaskFailListener

new JsonCake.Builder()
	.setUrl("your_json_file_url")
    .setOnFinishListener(new OnFinishLoadStringListener(){

		@Override
		public void onFinish(String arg0) {
			// TODO Auto-generated method stub
			/* write your code here */
		}

    })
    .setOnTaskFailListener(new OnTaskFailListener(){

		@Override
		public void onFail(String errorMessage, Exception exception) {
			// TODO Auto-generated method stub
			/* write your code here */
		}
    		
    })
    .get();

####Load Custom Object parsed by Gson

new JsonCake.Builder()
	.setUrl("your_json_file_url")
    .setObjectType(DataSet.class)
    .setOnFinishListener(new OnFinishLoadObjectListener(){

		@Override
		public void onFinish(Object object) {
			// TODO Auto-generated method stub
			DataSet dataSet = (DataSet)object;
			/* write your code here */
		}
    		
    })
    .get();

Note: DataSet是自己定義的class。Gson可以把下載的Json解析成DataSet Object。詳情請參考官網

####Send Http Post and Get String Response

RequestBody formBody = new FormEncodingBuilder()
	.add("Name", "Tom") // key-value pair in POST
    .add("Age", "25")
    .build();
        
new JsonCake.Builder()
	.setUrl("your_url")
    .setOnFinishListener(new OnFinishLoadStringListener(){

		@Override
		public void onFinish(String responseStr) {
			// TODO Auto-generated method stub
			/* write your code here */
		}
        		
    })        	
    .setFormBody(formBody)
    .post();

Note: RequestBody 是 OkHttp所定義的Class,可以依序將Form的資料依序填入。詳情請參考官網

Required Android Permission

<uses-permission android:name="android.permission.INTERNET">

License

Copyright 2015 David Ma

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages