在Android Studio中将gzip json解压缩为字符串

时间:2019-05-27 17:26:49

标签: android json gzip

我如何使此代码在Android Studio中解压缩Gzip?对于这个问题的第一个答案,我不知道需要更改什么。

Decompress gzip json response to string in Android

    try {

            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(UtilMediaEscolar.READ_TIMEOUT);
            conn.setConnectTimeout(UtilMediaEscolar.CONNECTION_TIMEOUT);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("charset", "utf-8");

            conn.setDoInput(true);
            conn.setDoOutput(true);

            conn.connect();

            String query = builder.build().getEncodedQuery();

            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(query);
            writer.flush();
            writer.close();
            os.close();
            conn.connect();


        } catch (IOException erro) {


            Log.e("WebService", "IOException - "+erro.getMessage());

        }

        try {

            int response_code = conn.getResponseCode();

            // 200 ok
            // 403 forbidden
            // 404 pg não encontrada
            // 500 erro interno no servidor

            if (response_code == HttpURLConnection.HTTP_OK) {

                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();

                String linha;

                while ((linha = reader.readLine()) != null) {
                    result.append(linha);
                }

                return (result.toString());

            } else {
                return ("Connection error");
            }

        } catch (IOException e) {

            Log.e("WebService","IOException - "+e.getMessage());

            return e.toString();
        } finally {

            conn.disconnect();
        }

我尝试使用此代码,但我不知道进行此更改...

                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(UtilMediaEscolar.URL_WEB_SERVICE);
                httpPost.setEntity(new StringEntity(result.toString(), "UTF-8"));
                httpPost.setHeader("api_key", "<TSssssssssssssaaa7>");
                httpPost.setHeader("Content-Type", "application/json");
                httpPost.setHeader("Accept-Encoding", "gzip");
                // execute request
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();

                // handle gzip compression
                Header contentEncodingHeader = entity.getContentEncoding();
                if (contentEncodingHeader != null) {
                    HeaderElement[] encodings = contentEncodingHeader.getElements();
                    for (HeaderElement encoding : encodings) {
                        if (encoding.getName().equalsIgnoreCase("gzip")) {
                            entity = new GzipDecompressingEntity(entity);
                            break;
                        }
                    }
                }

我更改了这一行:

conn.setRequestProperty("Accept-Encoding", "gzip");

LogCat返回以下内容:

E/WebService: Erro JSONException Value �������������� of type java.lang.String cannot be converted to JSONArray

0 个答案:

没有答案