2011-09-08 145 views
0

我正在構建一個需要使用post方法與服務器連接並獲取結果的應用程序。我需要服務器響應的特定部分並將其轉換爲不同類型的圖元(從Stringshortintbyte等)Java短原始類型問題

所以基本上我需要得到的響應代碼的一部分,並將其轉換來看看是否有一個enum元素與此值。但問題是,響應返回值001,並將其轉換爲短,並將其傳遞給我的getByValue(int)方法在enum,它說我沒有元素與001.如果我打印short值,我得到1

這裏是一個代碼示例我使用的:

   httppost.setEntity(new UrlEncodedFormEntity(postParameters)); 

      HttpResponse response = httpclient.execute(httppost); 
      Log.v("Response ","Status line : "+ response.getStatusLine().toString()); 
      String responseBody = EntityUtils.toString(response.getEntity()); //response 
      Log.v("Response ","Response : "+ responseBody); 

      int objectIdentificator = Integer.parseInt(responseBody.substring(0,32)); 
      Log.v("Response ","Object Identificator (LONGINT) : "+ responseBody.substring(0,32)); 
      Log.v("Response ","Object Identificator (LONGINT) : "+ objectIdentificator); 

      String type = responseBody.substring(32,35); 
      Log.v("Response ","TYPE (UNSIGNED BYTE) : "+ type); 
      short pType = Short.parseShort(type); // short 
      Log.v("Response ","TYPE (UNSIGNED BYTE) : "+ pType); 

      String operation = responseBody.substring(35,38); // 
      short operationType = Short.parseShort(operation); 
      Log.v("Response ","OPERATION (UNSIGNED BYTE) : "+ operation); 
      Log.v("Response ","OPERATION (UNSIGNED BYTE) : "+ operationType); 

      String objectId = responseBody.substring(38, 70); 
      Log.v("Response ","UID (CHAR, length 32) : "+ objectId); 

      int id = Integer.parseInt(responseBody.substring(70, 102)); 
      Log.v("Response ","ID (LONGINT) : "+ responseBody.substring(70, 102)); 
      Log.v("Response ","ID (LONGINT) : "+ id); 

      String size = responseBody.substring(102,134);   
      Log.v("Response ","Data Size (LONGINT) : "+ size); 

      String hash = responseBody.substring(134,166); 
      Log.v("Response ","Data Hash (CHAR, length 32 : "+ hash); 

      String dType = responseBody.substring(166,169); 
      Log.v("Response ","Data Type (UNSIGNED BYTE) : "+ dType); 
      short dataType = Short.parseShort(dType); 
      Log.v("Response ","Data Type (UNSIGNED BYTE) : "+ dataType); 

      String data = responseBody.substring(169, responseBody.length()); 
      Log.v("Response ","Data (CHAR, any length, in BASE64) : "+ data); 

      byte[] first = Base64.decode(data); 
      String string = new String(first, "UTF-8"); 

      Log.v("Response ","BASE 64 : "+ string); 


      RPCPacket packet = new RPCPacket( objectIdentificator, 
               RPCPacketType.getPacketTypeByValue(pType), 
               RPCOperationType.getByValue(Integer.parseInt(operation)), 
               objectId, 
               id, 
               Integer.parseInt(size), 
               hash, 
               RPCPacketDataType.getByValue(dataType), 
               first 
               ); 


      Log.v("PacketType", "RPCPacketType : "+packet.packetTypeToStr(RPCPacketType.getPacketTypeByValue(pType))); 

而且packetTypeToStr代碼:

public String packetTypeToStr(RPCPacketType type){ 

     String str=null; 
     switch(type){ 
     case ST_OBJECT_TYPE_INFO_START: 
       str = "ST_OBJECT_TYPE_INFO_START"; 
      break; 
     case ST_OBJECT_TYPE_INFO_ERROR: 
       str = "ST_OBJECT_TYPE_INFO_ERROR"; 
      break; 
     case ST_OBJECT_TYPE_COLLECTION: 
       str = "ST_OBJECT_TYPE_COLLECTION"; 
      break; 
     case ST_OBJECT_TYPE_CATEGORY: 
       str = "ST_OBJECT_TYPE_CATEGORY"; 
      break; 
     case ST_OBJECT_TYPE_CARD: 
       str = "ST_OBJECT_TYPE_CARD"; 
      break; 
     case ST_OBJECT_TYPE_MESSAGE: 
       str = "ST_OBJECT_TYPE_MESSAGE"; 
      break; 
     case ST_OBJECT_TYPE_GENRE: 
       str = "ST_OBJECT_TYPE_GENRE"; 
      break; 
     case ST_OBJECT_TYPE_TAG: 
       str = "ST_OBJECT_TYPE_TAG"; 
      break; 
     case ST_OBJECT_TYPE_USER: 
       str = "ST_OBJECT_TYPE_USER"; 
      break; 
     case ST_OBJECT_TYPE_MEDIA_COLLECTION: 
       str = "ST_OBJECT_TYPE_MEDIA_COLLECTION"; 
      break; 
     case ST_OBJECT_TYPE_MEDIA_CATEGORY: 
       str = "ST_OBJECT_TYPE_MEDIA_CATEGORY"; 
      break; 
     case ST_OBJECT_TYPE_MEDIA_CARD: 
       str = "ST_OBJECT_TYPE_MEDIA_CARD"; 
      break; 
     case ST_OBJECT_TYPE_MEDIA_TAG: 
       str = "ST_OBJECT_TYPE_MEDIA_TAG"; 
      break; 
     case ST_OBJECT_TYPE_INFO_END: 
       str = "ST_OBJECT_TYPE_INFO_END"; 
      break; 
     case ST_OBJECT_TYPE_CARDSTATS_CATEGORY: 
       str = "ST_OBJECT_TYPE_CARDSTATS_CATEGORY"; 
      break; 
     case ST_OBJECT_TYPE_CONTENT: 
       str = "ST_OBJECT_TYPE_CONTENT"; 
      break; 
     case ST_OBJECT_TYPE_MEDIA_COLLECTION_BUTTON: 
       str = "ST_OBJECT_TYPE_MEDIA_COLLECTION_BUTTON"; 
      break; 
     default: 
       str ="UNKNOWN "+type; 
      break; 
     } 

     return str; 
    } 

和異常:

09-08 09:53:08.744: WARN/System.err(2509): java.lang.IllegalArgumentException: no datatype with 001 exists 
09-08 09:53:08.754: WARN/System.err(2509):  at com.stampii.stampii.comm.rpc.RPCCommucatorDefines$RPCOperationType.getByValue(RPCCommucatorDefines.java:34) 
09-08 09:53:08.754: WARN/System.err(2509):  at com.stampii.stampii.user.UserLogin$2.onClick(UserLogin.java:122) 
09-08 09:53:08.754: WARN/System.err(2509):  at android.view.View.performClick(View.java:2408) 
09-08 09:53:08.754: WARN/System.err(2509):  at android.view.View$PerformClick.run(View.java:8817) 
09-08 09:53:08.754: WARN/System.err(2509):  at android.os.Handler.handleCallback(Handler.java:587) 
09-08 09:53:08.754: WARN/System.err(2509):  at android.os.Handler.dispatchMessage(Handler.java:92) 
09-08 09:53:08.754: WARN/System.err(2509):  at android.os.Looper.loop(Looper.java:144) 
09-08 09:53:08.754: WARN/System.err(2509):  at android.app.ActivityThread.main(ActivityThread.java:4937) 
09-08 09:53:08.754: WARN/System.err(2509):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-08 09:53:08.754: WARN/System.err(2509):  at java.lang.reflect.Method.invoke(Method.java:521) 
09-08 09:53:08.754: WARN/System.err(2509):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
09-08 09:53:08.764: WARN/System.err(2509):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
09-08 09:53:08.764: WARN/System.err(2509):  at dalvik.system.NativeStart.main(Native Method) 

RPCOperationType代碼:

public enum RPCOperationType { 
     O_CREATE(1), 
     O_UPDATE(2), 
     O_DELETE(3); 

     private int value; 
     private intvalue1; 
     private RPCOperationType(int i){ 
      this.value=i; 
     } 
     public int getNumericType(){ 
      return value; 
     } 
     public static RPCOperationType getByValue(int i) { 
      for(RPCOperationType dt : RPCOperationType.values()) { 
       if(dt.value1 == i) { 
        return dt; 
       } 
      } 
      throw new IllegalArgumentException("no datatype with " + i + " exists"); 
     } 

    } 

所以,任何建議如何解決這個問題,而不改變ID在枚舉? 在此先感謝!

+3

你已經向我們展示了枚舉的所有內容 - 例如你如何找到基於ID的枚舉值。在我看來,這是重要的一點... –

+0

我更新了我的問題 –

+1

你是說'拋出IllegalArgumentException'嗎?它是否帶有消息「沒有與001存在的數據類型」?這不太合理,因爲整數不會以這種方式打印。你能準確地告訴我們你看到了什麼嗎? –

回答

1

例外情況不會由您向我們顯示的代碼拋出。堆棧跟蹤說這裏發生異常:

at com.stampii.stampii.comm.rpc.RPCCommucatorDefines$RPCOperationType.getByValue(RPCCommucatorDefines.java:34) 

它因此RPCCommucatorDefines.java調用RPCOperationType.getByValue(),在拋出該異常線34。你肯定會把它傳遞給一個字符串,而不是一個int,因爲沒有辦法將一個短變量打印爲001,因爲它是在異常的錯誤消息中完成的。

堆棧軌跡的兩條第一行是最重要的一行。第一個問題是你怎麼了,第二個問題告訴你拋出異常的地方。

+0

在getByValue方法中引發的異常與問題中顯示的方法完全相同。 –

+0

按照我所說的,讀取堆棧跟蹤,並向我們展示RPCCommucatorDefines.java的第34行的RPCOperationType.getByValue方法的代碼。此代碼不可能與您向我們展示的代碼相同。 –

+0

我看到,在getByValue我採取字符串類型,不int..so我編輯它作爲字符串,現在錯誤說「沒有數據類型1存在」。 –

0

如果您需要用零填充數字,您必須使用使用Formatter