Android Question java.util.zip.ZipException: incorrect header check - JRDC2 B4J

Nataly

Member
Hello Everyone,

First of all, I know that this forum is for B4X development. However, currently, I'm working with Android studio and I want to connect my app to jrdc2 (B4J server). So I'm here because I'm stuck and need a little help that I couldn't find anywhere else.

So I implemented DBRequestManager & DBCommand just like in b4a. I try to send commands and parameters but keep getting the following error:

java.util.zip.ZipException: incorrect header check

This is my code. I would greatly appreciate it if someone could help and help me figure out where is the problem

DBCommand:
public class DBCommand implements Serializable {
    String Name;
    List<Object> Parameters;

    public DBCommand(String name, List<Object> parameters) {
        Name = name;
        Parameters = parameters;
    }
}

DBRequestManager:
public class DBRequestManager implements Serializable {
    private Object mTarget;
    private String link;
    private float VERSION = 2;

    
    public void initialize(Object target, String connectorLink) {
        mTarget = target;
        link = connectorLink;
    }


    public void ExecuteQuery(DBCommand command, int limit, Object tag) throws IOException {
        Map<String, Object> map = new HashMap<>();
        map.put("command", command);

        // Convert Map to byte array
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(map);

        byte[] data = byteOut.toByteArray();


        AsyncHttpClient httpClient = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        try {
            params.put("command", data);
            params.put("limit", limit);
            params.put("version", VERSION);
        } catch (Exception exception) {
            System.out.println("error");
        }


        httpClient.post(link, params, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                System.out.println("START");
            }

            @Override
            public void onFinish() {
                System.out.println("FINISH");
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                System.out.println("SUCCESS");
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                System.out.println("FAILED" + error.toString()); //this is where i get the error too
            }

        });


    }

}

And finally in MainActivity.Java

MainActivity:
        DBRequestManager dbRequestManager = new DBRequestManager();
        List<Object> temp = new ArrayList<>();
        temp.add("Name");
        DBCommand dbCommand = new DBCommand("getInfo", temp);

        dbRequestManager.initialize(MainActivity.this, "http://x.x.x.x:yyyy/rdc?method=query2");
        
        try {
            dbRequestManager.ExecuteQuery(dbCommand, 0, null);
        } catch (IOException e) {
            System.out.println("ERROR");
            e.printStackTrace();
        }
 

drgottjr

Expert
Licensed User
Longtime User
i don't think the error has anything to do with the code you posted.

java.util.zip.ZipException: incorrect header check refers to the
method used to compress and [un|de]-compress the files in a
zip archive. when compressing a file, a particular type of
compression scheme is used (there is a default scheme). a
tag indicating the scheme is put in the file's header (eg, type
8 for the default). when you go to unzip the archive, the unzipper
looks at that tag to see which method to use.

anyway, if you try to decompress a compressed stream using
java.util.zip, an exception will be thrown if the header is wrong.
this usually means the stream was compressed using gzip,not
zip. the stream has a signature (the first couple bytes tells you
which scheme was used, but java.util.zip doesn't handle this
automatically. i just looks for 1 signature. if that's not found,
it throws the exception. you have to tell it to look for a
different signature. that implies you're decompressing the
stream yourself, which, in this case, you're not.)

there is a way to deal with this,but there is no mention of a zip
archive in the code you posted. i would guess there is a
compressed stream somewhere and someone is using the standard
java.util.zip "inflate" method to uncompress it. it needs a
different version to uncompress a non-zipped stream.
 
Upvote 0
Top