4

我一直在尝试使用 Android 和BioJava查询NCBI 爆炸网站。我正在使用带有 Android 模拟器的 Eclipse。当我将代码作为 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 应用程序运行时,它可以完美运行。关于它可能是什么的任何想法?

4

1 回答 1

14

当我尝试在 Android 中发出 POST 请求时,我也发生了这种情况。如果您在标头中设置 Content-Length 并且实际内容具有不同的长度,它将IOException在 Android 中抛出一个(在普通的 JDK 中它可以工作,尽管它是错误的)

您可以使用以下代码重现异常:

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

所以如果你删除线

yc.setRequestProperty("Content-Length", "1000");

它会工作的!

于 2012-05-23T06:52:28.280 回答