Skip to content

crazyma/LoadJsonTask

Repository files navigation

介紹

客製化 AsyncTask, 專門用來Download Json 並可自行定義初步的解析規則 方便developer使用

Variable

####Required

  1. urlStr : 網址
  2. onFinishListener : Task 結束時所呼叫的interface

####Optional

  1. connectionTimeout : connection timeout 的時間(毫秒)
  2. socketTimeout : socket timeout 的時間(毫秒)
  3. onParseJsonListener : 當原始JSON Object下載完成, 可以定義初步的解析規則

Interface

####OnFinishLoadJsonListener ######有三個method :

  onFinish(JSONObject resultJsonObj)
  onFinish(JSONArray resultJsonAry)
  onFinish(String resultStr)

如果沒有設定 onParseJsonListener, 則會呼叫onFinish(JSONObject resultJsonObj), resultJsonObj 則會是從server上收到的完整 JSONObject

####OnParseJsonListener ######有一個method : onParse(JSONObject jsonObj)

  • 引數 jsonObj 是從server上收到的完整 JSONObject, 使用者可以在 onParse 裡定義自己的parsing規則
  • 回傳值可以為三種:JSONObject, JSONArray, String, 會分別呼叫對應的 onFinish Method
  • 非此三種的回傳值, 則不會呼叫任何一個 onFinish

簡易使用流程

  1. 宣告LoadJsonTask物件
  2. 設定 url 網址
  3. 設定 onFinishLoadJsonListener
  4. 呼叫 loadJsonTaskObject.execute()

--

	LoadJsonTask task = new LoadJsonTask();
	task.setUrlStr(" your url address ");
	task.setOnFinishLoadJsonListener(new OnFinishLoadJsonListener(){
		@override
		public onFinish(JSONObject resultJsonObj){
			//	do you jobs
		}
	});
	task.execute();

Required Permission

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

License

Copyright 2013 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