Android- Java.io.Exception:内容长度错误预期x记忆量达到y量

ket*_*ujo 4 java post android http

我一直在尝试使用Android和BioJava查询NCBI爆炸网站.我正在使用Eclipse和Android模拟器.当我将代码作为Android应用程序运行时,我收到以下错误:

W/System.err(533): java.io.IOException: An error occured submiting sequence to BLAST server. Cause: content-length promised 2000 bytes, but received 214

当我使用相同的代码并将其作为常规Java应用程序运行时,它可以完美地运行.关于它可能是什么的任何想法?

Chr*_*ios 14

在我尝试在Android中发出POST请求时,它也发生在我身上.如果你在标题中设置Content-Length并且实际内容有不同的长度,它将IOException在Android中抛出(在普通的JDK中,它有效,尽管它是错的)

您可以使用以下代码重现Exception:

try {
    URL oracle = new URL("http://oasth.gr/tools/lineTimes.php");
    HttpURLConnection yc = (HttpURLConnection) oracle.openConnection();

    yc.setRequestProperty("User-Agent",
            "Mozilla/5.0 (X11; Linux i686; rv:2.0b12) Gecko/20110222 Firefox/4.0b12");
    yc.setRequestProperty("Referer",
            "http://oasth.gr/tools/lineTimes.php");
    yc.setRequestProperty("Accept-Language",
            "el-gr,el;q=0.8,en-us;q=0.5,en;q=0.3");
    yc.setRequestProperty("Accept-Charset",
            "ISO-8859-7,utf-8;q=0.7,*;q=0.7");

    // content length should be 29 !!
    yc.setRequestProperty("Content-Length", "1000");

    // content length is 29 !
    String parames = "bline=63&goes=a&lineStops=886";

    yc.setDoOutput(true);

    OutputStreamWriter wr = new OutputStreamWriter(yc.getOutputStream());

    wr.write(parames);
    wr.flush();
    Log.d(TAG, "" + yc.getResponseCode());

    BufferedReader in = new BufferedReader(new InputStreamReader(
            yc.getInputStream()));
    String inputLine;

    StringBuilder sbu = new StringBuilder();

    while ((inputLine = in.readLine()) != null)
        sbu.append(inputLine);

    wr.close();
    in.close();

} catch (IOException e) {
    e.printStackTrace();
    Log.e("getLinesArrival Exception", e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)

所以,如果你删除线

yc.setRequestProperty("Content-Length", "1000");
Run Code Online (Sandbox Code Playgroud)

它会工作!