android json

package com.puyi.mobilehospital.http.orders;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Message;
import android.util.Log;

import com.puyi.mobilehospital.entity.costManager.CancelMedicine;
import com.puyi.mobilehospital.http.connection.HttpCommand;
import com.puyi.mobilehospital.utils.LogUtil;

/**
 * 
 * @类名称: CancelMedicineOrder 
 * @类描述:取消领药
 * @作 者: 胡高山
 * @创建时间: 2012-4-26 下午7:43
 */
public class CancelMedicineOrder implements HttpCommand {
	private static final String TAG = "CancelMedicineOrder";
	private String json;
	private String hospitalNo;

	@Override
	public void execute(JSONObject parameter, Message msg) throws Exception {
	
		// 填入假数据,模拟已经从服务器返回包含数据的Json,解析后通过Message传出,在界面由Handler接收后显示

		JSONObject jparameter = parameter;
		hospitalNo = (String) jparameter.get("hospitalNo");
		json = setJson(hospitalNo);

		if (null != json) {//有假数据

			Log.i(TAG, "execute setJson no null ");

			JSONObject jsnobj;
			try {
				jsnobj = new JSONObject(json);
				int code = jsnobj.optInt("code");
				String errormsg = jsnobj.optString("message");
				LogUtil.d(TAG, "code: " + code);
				if (code == 0) {
					ArrayList<CancelMedicine> cancelList = messageParse(json);
					Log.i(TAG, "messageParse json OK 53");
					msg.obj = cancelList;
					msg.arg1 = code;
				} else if (code == 1) {
					msg.obj = errormsg;
					msg.arg1 = code;
					Log.i(TAG, "messageParse json error 60");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		msg.sendToTarget();
	}

	/**
	 * 解析响应的JSON数据 PatientInfos
	 */
	public ArrayList<CancelMedicine> messageParse(String json) {

		ArrayList<CancelMedicine> cancelList=new ArrayList<CancelMedicine>();
		
		try {
			JSONObject jsnobj = new JSONObject(json);
			// 总Info
			JSONObject CancelMedicineInfo = jsnobj.optJSONObject("CancelMedicineInfo");
			
			JSONArray jaCancelMedicine=CancelMedicineInfo.optJSONArray("CancelMedicineList");
	

//取消领药 CancelMedicine======================================================================================
			if(jaCancelMedicine.length()>0){
				
				CancelMedicine cancelMedicine;
				
				for(int i=0;i<jaCancelMedicine.length();i++){
					JSONObject object=jaCancelMedicine.optJSONObject(i);
					cancelMedicine=new CancelMedicine();
					
					cancelMedicine.hospitalNo=object.optString("hospitalNo");
					cancelMedicine.productName=object.optString("productName");
					cancelMedicine.standard=object.optString("standard");
					cancelMedicine.price=object.optDouble("price");
					cancelMedicine.applyNum=object.optInt("applyNum");
					cancelMedicine.totalMoney=object.optDouble("totalMoney");
					cancelMedicine.applyLab=object.optString("applyLab");
					cancelMedicine.provideLab=object.optString("provideLab");
					cancelMedicine.patientName=object.optString("patientName");
					cancelMedicine.applyDate=object.optString("applyDate");
					
					cancelList.add(cancelMedicine);
				}
			}


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

	@Override
	public boolean onTimeOut(JSONObject parameter, Message msg) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onException(JSONObject parameter, Message msg, Exception e) {
		// TODO Auto-generated method stub

	}

	// 假数据填入方法
	public String setJson(String hospitalNo) {
		String jsonstr = null;
		if (hospitalNo.equals("00000002")) {
			jsonstr = "{\"code\":0,\"message\":\"success\",\"CancelMedicineInfo\":{"

					+ "\"CancelMedicineList\":["
					+ "{\"hospitalNo\": \"00000002\",\"productName\":\"生血宁片\",\"standard\":\"0.5mg片\",\"price\":\"7.5\",\"applyNum\":\"2\",\"totalMoney\":\"15\",\"applyLab\":\"05病区\",\"provideLab\":\"药房\",\"patientName\":\"秦春华\",\"applyDate\":\"2011-10-05 0:30:25\"},"
					+ "{\"hospitalNo\": \"00000002\",\"productName\":\"生血宁片\",\"standard\":\"1.5mg片\",\"price\":\"5.5\",\"applyNum\":\"2\",\"totalMoney\":\"11\",\"applyLab\":\"05病区\",\"provideLab\":\"药房\",\"patientName\":\"秦春华\",\"applyDate\":\"2011-10-05 0:30:25\"},"
					+ "{\"hospitalNo\": \"00000002\",\"productName\":\"生血宁片\",\"standard\":\"2.5mg片\",\"price\":\"3.5\",\"applyNum\":\"2\",\"totalMoney\":\"7\",\"applyLab\":\"05病区\",\"provideLab\":\"药房\",\"patientName\":\"秦春华\",\"applyDate\":\"2011-10-05 0:30:25\"},"
					+ "{\"hospitalNo\": \"00000002\",\"productName\":\"生血宁片\",\"standard\":\"2.5mg片\",\"price\":\"6.5\",\"applyNum\":\"2\",\"totalMoney\":\"13\",\"applyLab\":\"05病区\",\"provideLab\":\"药房\",\"patientName\":\"秦春华\",\"applyDate\":\"2011-10-05 0:30:25\"},"
					+ "{\"hospitalNo\": \"00000002\",\"productName\":\"生血宁片\",\"standard\":\"0.15mg片\",\"price\":\"2.5\",\"applyNum\":\"2\",\"totalMoney\":\"5\",\"applyLab\":\"05病区\",\"provideLab\":\"药房\",\"patientName\":\"秦春华\",\"applyDate\":\"2011-10-05 0:30:25\"}"
					+ "]"
					
					+"}}";
		} else if (hospitalNo.equals("00000001")) {
			jsonstr = "{\"code\":0,\"message\":\"success\",\"CancelMedicineInfo\":{"

					+ "\"CancelMedicineList\":["
					+ "{\"hospitalNo\": \"00000001\",\"productName\":\"利可君片\",\"standard\":\"2.5mg片\",\"price\":\"6.5\",\"applyNum\":\"2\",\"totalMoney\":\"13\",\"applyLab\":\"01病区\",\"provideLab\":\"药房\",\"patientName\":\"范子毅\",\"applyDate\":\"2011-10-05 0:30:25\"},"
					+ "{\"hospitalNo\": \"00000001\",\"productName\":\"利可君片\",\"standard\":\"1.5mg片\",\"price\":\"5.5\",\"applyNum\":\"2\",\"totalMoney\":\"11\",\"applyLab\":\"01病区\",\"provideLab\":\"药房\",\"patientName\":\"范子毅\",\"applyDate\":\"2011-10-05 0:30:25\"},"
					+ "{\"hospitalNo\": \"00000001\",\"productName\":\"利可君片\",\"standard\":\"2.5mg片\",\"price\":\"3.5\",\"applyNum\":\"2\",\"totalMoney\":\"7\",\"applyLab\":\"01病区\",\"provideLab\":\"药房\",\"patientName\":\"范子毅\",\"applyDate\":\"2011-10-05 0:30:25\"},"
					+ "{\"hospitalNo\": \"00000001\",\"productName\":\"利可君片\",\"standard\":\"2.5mg片\",\"price\":\"6.5\",\"applyNum\":\"2\",\"totalMoney\":\"13\",\"applyLab\":\"01病区\",\"provideLab\":\"药房\",\"patientName\":\"范子毅\",\"applyDate\":\"2011-10-05 0:30:25\"},"
					+ "{\"hospitalNo\": \"00000001\",\"productName\":\"生血宁片\",\"standard\":\"0.15mg片\",\"price\":\"2.5\",\"applyNum\":\"2\",\"totalMoney\":\"5\",\"applyLab\":\"01病区\",\"provideLab\":\"药房\",\"patientName\":\"范子毅\",\"applyDate\":\"2011-10-05 0:30:25\"}"
					+ "]"
					
					+"}}";
		}
		return jsonstr;
	}

}

你可能感兴趣的:(android,json,exception,object,String,null)