开发者

Java short primitive type issue

I'm building an application which requires connection with server with post method and gets the result. I need to get the particular part of the server response and convert it to different types of primitives (from String to short, int, byte etc.)

So basically I need to get a part of the response code and convert it to short, than see if there is an enum element with this value.But the problem is that the response returns value 001 and after I convert it to short and pass it to my getByValue(int) method in enum, it's saying me that there is no element with 001. If i print the short value I get 1.

Here is a sample of code which I'm using :

                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开发者_如何学C);

            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)));

And packetTypeToStr code :

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;
    }

And the exception :

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 code :

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");
         }

    }

So any suggestions how can I solve this issue without changing the id's in enum? Thanks in advance!


The exception is not thrown by the code you showed us. The stack trace says where the exception occurs:

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

It's thus the call to RPCOperationType.getByValue() in RPCCommucatorDefines.java, at line 34 that throws this exception. You're certainly passing it a String, and not an int, BTW, because there is no way for a short variable to be printed as 001 as it's done in the error message of the exception.

The two first lines of a stack traces are the most important one. The first one telle you what's wrong, and the second line tells you where the exception is thrown.


If you need to pad a number with zeros, you'll have to use Formatter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜