[英]NumberFormatException thrown when parsing String to Int
Am trying to parse a string to an int value after reading bytes from a stream and appending it to stringbuilder. 我试图从流中读取字节并将其附加到stringbuilder后,将字符串解析为int值。 But when I try to parse the string to an Int value the NumberFormatException is thrown.
但是,当我尝试将字符串解析为Int值时,将引发NumberFormatException。 Surrounding the statement with a "try and catch" doesn't help either as it parses " " which am guessing is null.
在语句周围加上“ try and catch”也无济于事,因为它会解析“”(猜测为空)。
Below is my code explaining my issue in detail: 以下是我的代码详细解释了我的问题:
String s = ClientStream.getInputStream(socks.getInputStream());
myDataSplitter splitter = myDataSplitter.getSplitStream(s);
/*
* Parse the page's length in bytes to an Int value.
* (Type)--Header
*/
int contIndx = Integer.parseInt(splitter.getContentLength("Content-Length:"));
The ClientStream.getInputStream()
method code: ClientStream.getInputStream()
方法代码:
public static String getInputStream(InputStream inputstream){
StringBuffer stringbuilder = new StringBuffer();
while(true) {
try {
rdL = inputstream.read();
} catch (IOException e) {
Log.e("InjectClientStream","Error reading byte from stream", e);
}
if(rdL == -1){
break;
}
stringbuilder.append((char) rdL);
if (stringbuilder.indexOf("\r\n\r\n") != -1) {
return stringbuilder.toString();
}
}
return stringbuilder.toString();
}
The myDataSplitter.getSplitStream()
method code: myDataSplitter.getSplitStream()
方法代码:
public static myDataSplitter getSplitStream(String str1) {
int i = 0;
myDataSplitter splitter = new myDataSplitter(str1);
String[] as = splitter.allInpRequest.split("\r\n",2);
if(as.length > 1) {
String[] as1 = as[1].replace("\r\n\r\n", "").split("\r\n");
if(i <= as1.length) {
i++;
String[] as2 = as1[i].split(":");
try {
splitter.map.put(as2[0], as2[1]);
}catch(Exception e) {
e.printStackTrace();
}
}
}
return splitter;
}
Then the method where the ERROR MESSAGE is raised.Code :: 然后引发错误消息的方法。
public String getContentLength(String paramString) {
//Returns the value of the mapped in string value
//"Content-Length"
String s = ((String)this.map.get(paramString));
if(s != null) {
return s;
}
return "";
}
What am I doing wrong? 我究竟做错了什么?
I think you need to try: 我认为您需要尝试:
String s = this.map.get(paramString).toString();
instead of 代替
String s = ((String)this.map.get(paramString))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.