在Android中它通过HTTP如何对COM preSS一个的JSONObject送?HTTP、Android、COM、JSONObject

2023-09-04 05:48:23 作者:请叫我村花

我发送的JSONObject 来我的网络服务器从 Android客户端利用this例如的。这里再现code

I am sending a JSONObject to my Webserver from an Android client using the code from this example. Reproducing code here

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

int TIMEOUT_MILLISEC = 10000;  // = 10 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);

HttpPost request = new HttpPost(serverUrl);
request.setEntity(new ByteArrayEntity(
    postMessage.toString().getBytes("UTF8")));
HttpResponse response = client.execute(request);

我的提问

如何把它发送到服务器,以及如何DECOM服务器上preSS之前做的JSONObject 最好的COM pression(我使用 Java小)?

How to do best compression on JSONObject before sending it to server and how to decompress it on Server (I am using Java Servlets)?

推荐答案

根据这个http://android-developers.blogspot.com/2011/09/androids-http-clients.html如果你正在使用姜饼或更高版本的HttpURLConnection会自动添加GZIP COM pression:

According to this http://android-developers.blogspot.com/2011/09/androids-http-clients.html If you are using Gingerbread or later HttpURLConnection automatically adds gzip compression:

在姜饼,我们增加了透明的回应COM pression。   HttpURLConnection的将这个头自动添加到传出   请求,并处理相应的响应:

In Gingerbread, we added transparent response compression. HttpURLConnection will automatically add this header to outgoing requests, and handle the corresponding response:

接受编码:gzip

您的Web服务器然后将需要处理的gzip COM pression。

Your webserver would then need to handle gzip compression.

编辑: 即成gzip压缩的内容与Java Servlet的

编辑2: Gzip已融为一体pression使用DefaultHttpClient 启用的GZip COM pression与HttpClient的

Edit 2: Gzip compression using DefaultHttpClient Enabling GZip compression with HttpClient

private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
private static final String ENCODING_GZIP = "gzip";

final DefaultHttpClient client = new DefaultHttpClient(manager, parameters);

client.addRequestInterceptor(new HttpRequestInterceptor() {
  public void process(HttpRequest request, HttpContext context) {
    // Add header to accept gzip content
    if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
      request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
    }
  }
});

client.addResponseInterceptor(new HttpResponseInterceptor() {
  public void process(HttpResponse response, HttpContext context) {
    // Inflate any responses compressed with gzip
    final HttpEntity entity = response.getEntity();
    final Header encoding = entity.getContentEncoding();
    if (encoding != null) {
      for (HeaderElement element : encoding.getElements()) {
        if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
          response.setEntity(new InflatingEntity(response.getEntity()));
          break;
        }
      }
    }
  }
});

编辑3: 下面是关于内容后的GZip POST请求的gzip压缩用了HTTPClient在Java中的另一个问题#1 。您需要发布前手动gzip压缩的数据,因为普通的HTTP / GZIP操作的服务器发送gzip压缩内容到客户端。

Edit 3: Here is another Stackoverflow question regarding the gzipping of post contents GZip POST request with HTTPClient in Java. You will need to manually gzip the data before posting it, since the normal http/gzip operation is the server sending gzipped content to the client.