我正在构建一个需要使用post方法与服务器连接的应用程序并获取结果。我需要获取服务器响应的特定部分并将其转换为不同类型的基元(从String
到short
,int
,byte
等。)
所以基本上我需要获取响应代码的一部分并将其转换为short,而不是查看是否存在具有此值的enum
元素。但问题是响应返回值{{1}并且在我将其转换为short并将其传递给001
中的getByValue(int)
方法后,它告诉我没有001的元素。如果我打印enum
值,我得到{ {1}}。
以下是我正在使用的代码示例:
short
和packetTypeToStr代码:
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)));
RPCOperationType代码:
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;
}
所以有什么建议如何在不改变枚举中的id的情况下解决这个问题? 提前谢谢!
答案 0 :(得分:1)
您向我们展示的代码不会引发异常。堆栈跟踪说明异常发生的位置:
at com.stampii.stampii.comm.rpc.RPCCommucatorDefines$RPCOperationType.getByValue(RPCCommucatorDefines.java:34)
因此,RPCOperationType.getByValue()
中RPCCommucatorDefines.java
的调用,在第34行抛出此异常。你当然传递一个字符串,而不是一个int,BTW,因为没有办法将短变量打印为001
,就像在异常的错误消息中一样。
堆栈跟踪的两个第一行是最重要的一行。第一行告诉你什么是错的,第二行告诉你抛出异常的位置。
答案 1 :(得分:0)
如果您需要用零填充数字,则必须使用Formatter 。