/*
 * Copyright 2012 Yu AOKI
 */

package com.aokyu.dev.pocket;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONException;
import org.json.JSONObject;

import com.aokyu.dev.pocket.util.JSONUtils;

public class AccessToken implements Serializable {

    private static final long serialVersionUID = 5522663727862182215L;

    public static final String KEY_TOKEN = "access_token";
    public static final String KEY_USERNAME = "username";
    private static final String ENCODE = "UTF-8";

    private String mToken;
    private String mUsername;

    public AccessToken(String token, String username) {
        mToken = token;
        mUsername = username;
    }

    /* package */ AccessToken(String response) {
        // String#split() is available in JDK1.4 or later.
        String[] elements = response.split("&");
        mToken = obtainParameter(elements, KEY_TOKEN);
        mUsername = obtainParameter(elements, KEY_USERNAME);
    }

    /* package */ AccessToken(JSONObject jsonObj) throws JSONException {
        String[] keys = JSONUtils.getKeys(jsonObj);
        int size = keys.length;
        for (int i = 0; i < size; i++) {
            String key = keys[i];
            if (key.equals(KEY_TOKEN)) {
                mToken = jsonObj.getString(KEY_TOKEN);
                continue;
            } else if (key.equals(KEY_USERNAME)) {
                mUsername = jsonObj.getString(KEY_USERNAME);
                continue;
            }
        }
    }

    private String obtainParameter(String[] elements, String key) {
        String value = null;
        for (String element : elements) {
            if (element.startsWith(key + '=')) {
                value = element.split("=")[1].trim();
                break;
            }
        }
        return value;
    }

    public String get() {
        return mToken;
    }

    public String getUrlEncoded() {
        String encoded = null;
        try {
            encoded = URLEncoder.encode(mToken, ENCODE);
        } catch (UnsupportedEncodingException e) {
            encoded = mToken;
        }
        return encoded;
    }

    public String getUsername() {
        return mUsername;
    }
}

@@ -253,22 +253,29 @@
 ket.
-util.JSONUtils
+error.PocketException
 ;%0A%0Ap
@@ -398,29 +398,30 @@
 215L;%0A%0A    p
-ublic
+rivate
  static fina
@@ -462,21 +462,22 @@
 %22;%0A    p
-ublic
+rivate
  static 
@@ -1079,20 +1079,22 @@
  throws 
-JSON
+Pocket
 Exceptio
@@ -1109,185 +1109,45 @@
     
-S
 tr
-ing%5B%5D keys = JSONUtils.getKeys(jsonObj);%0A        int size = keys.length;%0A        for (int i = 0; i %3C size; i++) %7B%0A            String key = keys%5Bi%5D;%0A            if (key.equals
+y %7B%0A            if (!jsonObj.isNull
 (KEY
@@ -1228,55 +1228,42 @@
     
-    continue;%0A            %7D else if (key.equals
+%7D%0A%0A            if (!jsonObj.isNull
 (KEY
@@ -1353,35 +1353,189 @@
     
-    continue;%0A            %7D
+%7D%0A        %7D catch (JSONException e) %7B%0A            throw new PocketException();%0A        %7D%0A%0A        if (mToken == null %7C%7C mUsername == null) %7B%0A            throw new PocketException();
 %0A   

Modify the implementation for parsing JSON data in AccessToken class