Java tutorial
/* * Copyright (C) 2013 Google Inc. * * 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 com.tbggwp.imit; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; /** * @author Mads Mello */ public class ImitTurn { public static final String TAG = "IMTurn"; public String data = ""; public int turnCounter; public ImitTurn() { } // This is the byte array we will write out to the Turn-based multiplayer API. public byte[] persist() { JSONObject retVal = new JSONObject(); try { retVal.put("data", data); retVal.put("turnCounter", turnCounter); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } String st = retVal.toString(); Log.d(TAG, "==== PERSISTING\n" + st); return st.getBytes(Charset.forName("UTF-16")); } // Creates a new instance of ImitTurn. static public ImitTurn unpersist(byte[] byteArray) { if (byteArray == null) { Log.d(TAG, "Empty array---possible bug."); return new ImitTurn(); } String st = null; try { st = new String(byteArray, "UTF-16"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); return null; } Log.d(TAG, "====UNPERSIST \n" + st); ImitTurn retVal = new ImitTurn(); try { JSONObject obj = new JSONObject(st); if (obj.has("data")) { retVal.data = obj.getString("data"); } if (obj.has("turnCounter")) { retVal.turnCounter = obj.getInt("turnCounter"); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return retVal; } }