Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android- Java.io.Exception: content Length error Expected x amount of memeory got y amount

I've been trying to query NCBI blast website using Android and BioJava. I'm using Eclipse with the Android emulator. When I run the code as an Android app I get the following errors:

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

When I take the very same code and run it as a regular Java app it works perfectly. Any ideas on what it might be?

like image 246
ketsujo Avatar asked Dec 10 '22 00:12

ketsujo


1 Answers

It also occured to me while I was trying to make a POST request in Android . If you set Content-Length in headers and the actual content has a different length, it will throw an IOException in Android (in plain JDK it worked, although it is wrong)

You can reproduce the Exception using the following code:

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

so if you remove line

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

it will work!

like image 99
Christos Manios Avatar answered Dec 13 '22 23:12

Christos Manios