客製化 AsyncTask, 專門用來Download Json 並可自行定義初步的解析規則 方便developer使用
####Required
- urlStr : 網址
- onFinishListener : Task 結束時所呼叫的interface
####Optional
- connectionTimeout : connection timeout 的時間(毫秒)
- socketTimeout : socket timeout 的時間(毫秒)
- onParseJsonListener : 當原始JSON Object下載完成, 可以定義初步的解析規則
####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
- 宣告LoadJsonTask物件
- 設定 url 網址
- 設定 onFinishLoadJsonListener
- 呼叫 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();
<uses-permission android:name="android.permission.INTERNET">
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.