Take the 2-minute tour ×
Stack Apps is a question and answer site for apps, scripts, and development with the Stack Exchange API. It's 100% free, no registration required.

I'm trying to receive a response for a regular api call like https://api.stackexchange.com/2.1/me/associated?key=myKey&access_token=myAccessToken

As a response I get something like ����������������� �_��l��|[U9��J����b qPmp轢���N�m�x�M������=�A����...

I think everything works okay this far, as the response should be GZIPped. But when trying to decompress the String I get an error: java.io.IOException: unknown format (magic number ef1f) (thrown by GZIPInputStream).

Currently I am using the following class:

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.zip.GZIPInputStream;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.util.JsonReader;
import android.util.Log;

public class RetrieveAccounts extends AsyncTask<Void, String, String> {

    protected String myFeed = "";
    String retrieveAddress = "https://api.stackexchange.com/2.1/me/associated?key=myKey&access_token=";

    public RetrieveAccounts(String accessToken) {
        retrieveAddress += accessToken;
        Log.i("URL", "Retrieve Address: " + retrieveAddress);
    }

    public ArrayList<String> readMessagesArray(JsonReader reader) throws IOException {
        ArrayList<String> messages = new ArrayList<String>();

        reader.beginArray();
        while (reader.hasNext()) {
            if(reader.nextName().equals("site_name")) {
                messages.add(reader.nextString());
            }
        }
        reader.endArray();
        return messages;
    }

    @Override
    protected String doInBackground(Void... params) {
        Log.i("URL", "doInBackground");
        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(retrieveAddress);
        httpGet.setHeader("Accept-Encoding", "GZIP");
        for(int i = 0; i < httpGet.getAllHeaders().length; i++) {
            Log.i("URL", "Header " + i + ": " + httpGet.getAllHeaders()[i].getValue());
        }
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();

            if(statusCode == 200) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
            }
            else {
                Log.e("URL", "Failed to download file");
            }
        }
        catch (ClientProtocolException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }

    @Override
    protected void onPostExecute(String result) {       
        String meinErgebnis = "";
        try {
            meinErgebnis = decompress(result.getBytes());
        }
        catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.i("URL", "Ergebnis: " + meinErgebnis);
    }

    public static String decompress(byte[] bytes) throws UnsupportedEncodingException, IOException {
//        if (str == null || str.length() == 0) {
//            return str;
//        }
        System.out.println("Input String length : " + bytes.length);
        GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes));
        BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
        String outStr = "";
        String line;
        while ((line=bf.readLine())!=null) {
          outStr += line;
        }
        System.out.println("Output String lenght : " + outStr.length());
        return outStr;
     }

}

What could be the problem here?

share|improve this question
add comment

You must log in to answer this question.

Browse other questions tagged .