2012-04-10 59 views
0

在該示例代碼的使用在應用程序內結算,它使用的Android的應用程序內結算 - 枚舉

public enum ResponseCode { 
    RESULT_OK, 
    RESULT_USER_CANCELED, 
    RESULT_SERVICE_UNAVAILABLE, 
    RESULT_BILLING_UNAVAILABLE, 
    RESULT_ITEM_UNAVAILABLE, 
    RESULT_DEVELOPER_ERROR, 
    RESULT_ERROR; 

    // Converts from an ordinal value to the ResponseCode 
    public static ResponseCode valueOf(int index) { 
     ResponseCode[] values = ResponseCode.values(); 
     if (index < 0 || index >= values.length) { 
      return RESULT_ERROR; 
     } 
     return values[index]; 
    } 
} 

int responseCode = response.getInt(Consts.BILLING_RESPONSE_RESPONSE_CODE); 
boolean billingSupported = (responseCode == ResponseCode.RESULT_OK.ordinal()); 

對我來說似乎是很奇怪,在這裏使用一個枚舉。如果枚舉使用了一些不同的順序,它全部失敗,我認爲枚舉不應該依賴於某個序數值。爲什麼這樣做,而不是隻檢查返回碼是否爲零?

在Android的文檔中指定例如返回代碼3爲RESULT_SERVICE_UNAVAILABLE。我只能從示例代碼中猜出這個。

感謝,A

回答

0

實際JSON responseCode是int,所以你需要將其轉換爲一個枚舉在Java中使用它。他們使用枚舉來表明有一組有限的響應代碼。當然,它可能是一堆final static int的,但這不太可讀。你引用的代碼可能已經被寫成這樣,這可能會更好地證明使用枚舉:

ResponseCode responseCode = ResponseCode.valueOf(response.getInt(Consts.BILLING_RESPONSE_RESPONSE_CODE)); 
boolean billingSupported = (responseCode == ResponseCode.RESULT_OK); 

至於文件,這是標準的Java行爲:枚舉從0開始,如果你不指定明確的價值。參看http://docs.oracle.com/javase/6/docs/api/java/lang/Enum.html#ordinal%28%29

IAB計費響應代碼等在這裏列出:http://developer.android.com/guide/market/billing/billing_reference.html