2011-09-08 2 views
0

post 메서드를 사용하여 서버와 연결해야하는 응용 프로그램을 빌드하고 결과를 얻습니다. 나는 그래서 기본적으로 내가 응답 코드의 일부를 얻고 그것을 변환해야합니다 (int, byte 등을 shortString에서)Java short primitive type 문제

을 서버 응답의 특정 부분을 얻을 프리미티브의 다른 유형으로 변환 할 필요가 이 값으로 enum 요소가 있는지보다 짧습니다. 그러나 문제는 응답이 001을 반환하고 단축으로 변환 한 다음 enum에있는 내 getByValue(int) 메서드로 전달하면 요소가 없다는 것입니다. 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이있는 데이터 유형이 없습니다"라는 메시지와 함께 표시됩니까? int가 그러한 방식으로 인쇄하지 않기 때문에 이것은 이해가되지 않습니다. 당신이 보는 것을 정확히 말해 줄 수 있습니까? –

답변

1

우리가 보여준 코드는 예외가 아닙니다.

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

그것은이 예외를 throw 라인 (34)에 따라서 RPCCommucatorDefines.java에서 RPCOperationType.getByValue()에 대한 호출입니다 : 예외가 발생하는 위치 스택 추적 말한다. 짧은 변수가 001으로 인쇄 될 수있는 방법이 없으므로 예외의 오류 메시지 에서처럼 String, int가 아닌 BTW를 확실히 전달합니다.

스택 추적의 두 개의 첫 번째 줄이 가장 중요합니다. 첫 번째 것은 잘못된 것을 telle하고, 두 번째 줄은 예외가 던져지는 곳을 알려줍니다.

+0

질문에 표시된 것과 정확히 동일한 getByValue 메서드에서 예외가 throw됩니다. –

+0

아니요. 제가 말했던 것처럼 스택 추적을 읽고 RPCOperationType.getByValue 메서드의 코드를 보여줍니다.이 코드는 RPCCommucatorDefines.java의 34 번째 줄에 있습니다. 이 코드는 우리에게 보여준 코드와 동일하지 않습니다. –

+0

getByValue에서 int 타입이 아닌 int 타입을 취하는 것을 보았습니다. 문자열로 편집하면 "no datatype with 1 exists"라고 표시됩니다. –

0

숫자를 0으로 채우려면 Formatter으로 사용해야합니다.

관련 문제