Java tutorial
/* * Copyright (C) 2012 CONIT Co., Ltd. * * 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. */ package jp.co.conit.sss.sp.ex1.io; import java.util.HashMap; import java.util.Map; import jp.co.conit.sss.sp.ex1.entity.SPResult; import org.json.JSONException; import org.json.JSONObject; import android.content.Context; /** * JSON????<br> * SamuraiPurchase????JSON???????????? <br> * ????{@code parce} ????<br> * <br> * ?????{@code getContent}???<br> * {@code getContent} ??JSON????<br> * ???????<br> * ???????????????? * * @author conit * @param <T> */ abstract class AbstSSSSPParser<T> { Context mContext; AbstSSSSPParser(Context context) { mContext = context; } public SPResult<T> getResult(String str) { // SamuraiPurchaseAPI?? Map<String, String> statusMap = parseStatus(str); if (isSamuraiApiErr(statusMap)) { String statusCode = statusMap.get("code"); String message = statusMap.get("message"); return SPResult.getErrorInstance(statusCode, message); } return parse(str); } /** * SamuraiParchace?WebAPI??????????<br> * ??????????0????????? * * @param statusMap * @return */ private boolean isSamuraiApiErr(Map<String, String> statusMap) { return (statusMap.size() == 0) ? false : true; } /** * SamuraiParchace?WebAPI?????<br> * ???????Map???? * * @param str * @return */ private Map<String, String> parseStatus(String str) { Map<String, String> resultMap = new HashMap<String, String>(); if (str == null) { return resultMap; } try { JSONObject jsonObject = new JSONObject(str); if (!jsonObject.has("error_info")) { return resultMap; } JSONObject databaseObject = jsonObject.getJSONObject("error_info"); String statusCode = databaseObject.getString("code"); resultMap.put("code", statusCode); String message = databaseObject.getString("message"); resultMap.put("message", message); } catch (JSONException e) { resultMap.clear(); return resultMap; } return resultMap; } /** * ?API????JSON??????? * * @param str * @return */ abstract SPResult<T> parse(String str); }