【问题标题】:How to send httpRequest to server with GZIP DATA如何使用 GZIP DATA 向服务器发送 http 请求
【发布时间】:2014-07-08 15:00:31
【问题描述】:

我有一个 json 文件,我将它作为 POST 发送到服务器,但它必须被压缩

我不知道该怎么做

我在这里找到了潜在的解决方案GZip POST request with HTTPClient in Java

但我不知道如何将他们在答案第二部分中使用的方法与我的 makeHttpRequest 方法合并(他们使用的是多部分实体,而我使用的是 urlencoded 实体)

编辑:这是我获取 jsonAsBytes 的方式

public static byte[] stringToGZIPByteArray (String string) {
    Log.d("string to be gzipped", string);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = null;

    try {
        gzos = new GZIPOutputStream(baos);
        gzos.write(string.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (gzos != null) {
            try { 
                gzos.close();
                } catch (IOException ignore) {

                };
        }
    }

    return baos.toByteArray();
} // End of stringToGZIPByteArray

这是我使用该方法的地方

jsonParser.sendGzippedJSONviaHTTP(context, API.JSON_ACCEPT,    UtilityClass.stringToGZIPByteArray(jsonObject.toString()), context.getResources());

这是 sendGzippedJSONviaHTTP

public JSONObject sendGzippedJSONviaHTTP(Context context, String url, byte[] gzippedJSON, Resources res) {

    if (httpClient == null) {
        try {
            httpClient = new HttpClientBuilder().setConnectionTimeout(10000) 
                    .setSocketTimeout(60000) //
                    .setHttpPort(80)//
                    .setHttpsPort(443)//
                    .setCookieStore(new BasicCookieStore())//
                    .pinCertificates(res, R.raw.keystore, null) //
                    .build();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // Making HTTP request
    try {

        // request method is POST
        // defaultHttpClient

        HttpPost httpPost = new HttpPost(url);


        httpPost.setHeader("Content-Encoding", "gzip");
        httpPost.setHeader("Content-Type", "application/json");

        httpPost.setEntity(AndroidHttpClient.getCompressedEntity(gzippedJSON, context.getContentResolver()));


        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        inputStream = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        inputStream.close();
        reader.close();
        json = sb.toString();
    } catch (ClientProtocolException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // try parse the string to a JSON object
    try {
        jsonObject = new JSONObject(json);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    // return JSON String
    return jsonObject;

} // End of makeHttpRequest

【问题讨论】:

    标签: java android


    【解决方案1】:

    看看AndroidHttpClient。您可以使用它来代替 appache 的 DefaultHttpClient。它有一个静态方法getCompressedEntity(byte[] data, ContentResolver resolver)

    所以,你可以写:

     HttpPost httpPost = new HttpPost(url);
     post.setEntity( AndroidHttpClient.getCompressedEntity( jsonAsBytes, null ) );
     httpClient.execute(httpPost);
    

    更新:

    这是来自AndroidHttpClient的代码:

    public static AbstractHttpEntity getCompressedEntity(byte data[], ContentResolver resolver)
            throws IOException {
        AbstractHttpEntity entity;
        if (data.length < getMinGzipSize(resolver)) {
            entity = new ByteArrayEntity(data);
        } else {
            ByteArrayOutputStream arr = new ByteArrayOutputStream();
            OutputStream zipper = new GZIPOutputStream(arr);
            zipper.write(data);
            zipper.close();
            entity = new ByteArrayEntity(arr.toByteArray());
            entity.setContentEncoding("gzip");
        }
        return entity;
    }
    

    应该会给你一些见解

    【讨论】:

    • 谢谢,当我有机会试一试时,我会告诉你发生了什么
    • 我不知道为什么,但这不起作用。服务器人员告诉我我发送的格式不正确。
    • 从服务器获取 gzip 文件并尝试对其进行 ungzip 和 JSON 解析
    • 服务器返回类似 [43@908a7sf
    • 仅此而已?如果是这样,那么这是toString() 表示byte[]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 2017-04-12
    相关资源
    最近更新 更多