Java tutorial
package kr.moonlightdriver.app.server; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /** * Result - Restor? JSON ? API ? ? ? * * Copyright 2013 Hyojun Kim. * * 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. */ public class Result { public static final String SUCCESS = "success"; public static final String FAILED_ACCOUNT_INVALID = "failed_account_invalid"; public static final String FAILED_ALREADY_EXISTS = "failed_already_exists"; // (: ?? ? ? ?) public static final String FAILED_TOKEN_INVALID = "failed_token_invalid"; // (: ? ?) public static final String FAILED_DB_CONNECTION = "failed_db_connnection"; // (: ?? ) // ? ? public JSONObject mJson; public Result() { mJson = new JSONObject(); } public Result(JSONObject json) { mJson = json; } public Result(String packet) { try { mJson = new JSONObject(packet); } catch (JSONException e) { Logg.e(packet); e.printStackTrace(); } } public String getString(String key) { String result = null; try { result = mJson.getString(key); } catch (JSONException e) { e.printStackTrace(); } catch (NullPointerException e) { Logg.e("Parameter fault : " + key + " = null (String)"); return null; } return result; } public int getInt(String key) { int result = -1; try { result = mJson.getInt(key); } catch (JSONException e) { e.printStackTrace(); } catch (NullPointerException e) { Logg.e("Parameter fault : " + key + " = null (Integer)"); return 0; } return result; } public Long getLong(String key) { Long result = new Long(-1); try { result = mJson.getLong(key); } catch (JSONException e) { e.printStackTrace(); } catch (NullPointerException e) { Logg.e("Parameter fault : " + key + " = null (Long)"); return null; } return result; } public JSONArray getJsonArray(String key) { JSONArray result = null; try { result = mJson.getJSONArray(key); } catch (JSONException e) { e.printStackTrace(); } catch (NullPointerException e) { Logg.e("Parameter fault : " + key + " = null (JSONArray)"); return null; } return result; } @Override public String toString() { return mJson.toString(); } /** * API ? ? * @return */ public boolean isSucceed() { if (getString("result") == null) { Logg.e("REST Failed : Internal Server error"); return false; } else if (getString("result").equals(SUCCESS)) { return true; } else { Logg.e("REST Failed : " + getString("result")); return false; } } /** * * @return */ public String getResultCode() { return getString("result"); } }