/*
 * 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 RequestToken implements Serializable {

    private static final long serialVersionUID = -9155567621462690980L;

    public static final String KEY_CODE = "code";
    private static final String ENCODE = "UTF-8";

    private String mToken;

    public RequestToken(String token) {
        mToken = token;
    }

    RequestToken(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_CODE)) {
                mToken = jsonObj.getString(KEY_CODE);
                continue;
            }
        }
    }

    public String get() {
        return mToken;
    }

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

@@ -253,22 +253,29 @@
 ket.
-util.JSONUtils
+error.PocketException
 ;%0A%0Ap
@@ -404,21 +404,22 @@
 ;%0A%0A    p
-ublic
+rivate
  static 
@@ -603,16 +603,30 @@
   %7D%0A%0A   
+ /* package */
  Request
@@ -658,20 +658,22 @@
  throws 
-JSON
+Pocket
 Exceptio
@@ -688,198 +688,203 @@
     
-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(KEY_CODE)) %7B
+y %7B%0A            if (!jsonObj.isNull(KEY_CODE)) %7B%0A                mToken = jsonObj.getString(KEY_CODE);%0A            %7D%0A        %7D catch (JSONException e) %7B%0A            throw new PocketException();
 %0A   
@@ -884,32 +884,35 @@
 ();%0A        
+%7D%0A%0A
         
 mToken = jso
@@ -895,32 +895,36 @@
  %7D%0A%0A        
+if (
 mToken =
  jsonObj.get
@@ -915,77 +915,58 @@
 en =
- jsonObj.getString(KEY_CODE);%0A                continue;%0A            %7D
+= null) %7B%0A            throw new PocketException();
 %0A   

Modify the implementation for parsing JSON data in RequestToken class