Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用GZIP数据向服务器发送httpRequest_Java_Android - Fatal编程技术网

Java 如何使用GZIP数据向服务器发送httpRequest

Java 如何使用GZIP数据向服务器发送httpRequest,java,android,Java,Android,我有一个json文件,作为POST发送到服务器,但它必须被gzip压缩 我不知道怎么做 我在这里找到了潜在的解决方案 但是我不知道如何将他们在回答的第二部分中使用的方法与我的makeHttpRequest方法合并(他们使用的是多部分实体,Im使用的是urlencoded实体) 编辑:下面是我获取jsonAsBytes的方法 public static byte[] stringToGZIPByteArray (String string) { Log.d("string to be gz

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

我不知道怎么做

我在这里找到了潜在的解决方案

但是我不知道如何将他们在回答的第二部分中使用的方法与我的makeHttpRequest方法合并(他们使用的是多部分实体,Im使用的是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
看一看。您可以使用它代替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;
}
public静态抽象HttpEntity getCompressedEntity(字节数据[],内容解析程序解析程序)
抛出IOException{
抽象http实体;
if(data.length

应该给你一些见解

谢谢,我会让你知道发生了什么,当我有机会尝试一下我不知道为什么,但这不起作用。服务器人员告诉我我发送的格式不对。从服务器人员那里获取gzip文件,然后尝试将其解压缩并进行JSON解析。服务器返回类似[43@908a7sfis如果是这样,那么这就是
toString()
字节[]的表示形式