将String解析为Int时抛出NumberFormatException

时间:2014-12-14 17:44:48

标签: java android

尝试在从流中读取字节并将其附加到stringbuilder后将字符串解析为int值。但是当我尝试将字符串解析为Int值时,抛出NumberFormatException。 用“try and catch”围绕语句也无济于事,因为它解析了“猜测为空”。

以下是我的代码详细解释我的问题:

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

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

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

然后引发 ERROR MESSAGE 的方法.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 ""; 
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我想你需要尝试:

String s = this.map.get(paramString).toString();

而不是

String s = ((String)this.map.get(paramString))