gpt4 book ai didi

Java短基本类型问题

转载 作者:行者123 更新时间:2023-12-01 15:44:33 25 4
gpt4 key购买 nike

我正在构建一个应用程序,需要使用 post 方法与服务器连接并获取结果。我需要获取服务器响应的特定部分并将其转换为不同类型的原语(从 Stringshortint字节等)

所以基本上我需要获取响应代码的一部分并将其转换为短,而不是查看是否有具有此值的 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;
}

异常(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 代码:

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 的情况下解决这个问题吗?提前致谢!

最佳答案

您向我们展示的代码并未引发异常。堆栈跟踪显示异常发生的位置:

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

因此调用 RPCOperationType.getByValue()RPCCommucatorDefines.java ,在第 34 行抛出此异常。顺便说一句,您肯定要向它传递一个字符串,而不是一个整数,因为短变量无法打印为 001正如异常的错误消息中所做的那样。

堆栈跟踪的前两行是最重要的一行。第一行告诉您出了什么问题,第二行告诉您抛出异常的位置。

关于Java短基本类型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7344085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com