根据json执行本地方法

2017-02-20 08:54
package org.qiling.helper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.qiling.activity.BrowserActivity;

public class ActHelper{
	/**
	 * 服务器返回数据回调本地方法
	 * @param json
     */
	static public void callback(final Activity context,String json) {
		try {
			//判断是json数组还是json对象
			if (json.subSequence(0, 1).equals("[")) {
				JSONArray arr = new JSONArray(json);
				for (int i = 0; i < arr.length(); i++) {
					run(context,arr.getJSONObject(i));
				}
			} else {
				run(context,new JSONObject(json));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 每条数据依次执行此方法
	 * @param obj
     */
	static public void run(final Activity context,JSONObject obj ){
		try {
			String act = obj.getString("act");
			switch (act){
				case "toast":
					UIHelper.showToast(obj.getString("msg"));
					break;
				case "dialog":
					UIHelper.showAlert(context,obj.getString("msg"));
					break;
				case "web":
					Intent intent = new Intent(context, BrowserActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("title",obj.getString("title"));
					bundle.putString("url", obj.getString("url"));
					intent.putExtras(bundle);
					context.startActivity(intent);
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

}
^