- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 this example 中的代码从 Android 客户端
向我的网络服务器发送一个 JSONObject
.在此处重现代码
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);
我的问题
如何在将 JSONObject
发送到服务器之前对其进行最佳压缩,以及如何在服务器上对其进行解压缩(我正在使用 Java Servlets
)?
最佳答案
根据这个http://android-developers.blogspot.com/2011/09/androids-http-clients.html如果您使用 Gingerbread 或更高版本的 HttpURLConnection 会自动添加 gzip 压缩:
In Gingerbread, we added transparent response compression. HttpURLConnection will automatically add this header to outgoing requests, and handle the corresponding response:
Accept-Encoding: gzip
然后您的网络服务器需要处理 gzip 压缩。
编辑:
Serve Gzipped content with Java Servlets
编辑 2:
使用 DefaultHttpClient 进行 Gzip 压缩 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 的另一个 Stackoverflow 问题 GZip POST request with HTTPClient in Java .您需要在发布数据之前手动对数据进行 gzip 压缩,因为正常的 http/gzip 操作是服务器向客户端发送 gzip 压缩后的内容。
关于android - 如何压缩 JSONObject 在 Android 中通过 Http 发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11402813/
我找不到答案。尝试了很多网站,包括这个。我想知道问题出在哪里。 错误是:org.json.JSONException:java.lang.String类型的值连接无法转换为JSONObject 这是我
我从 Java 应用程序提交了一个查询,该查询在 Elasticsearch 服务器上运行时以字符串形式返回结果。我希望结果为 JSONObject 列表对象。我可以将字符串转换为 JSONObjec
我正在尝试解析 JSON 字符串,但在尝试获取嵌套对象时出现错误: JSONObject jsonObject = new JSONObject(jsonString); System.out.pri
我有这样的 API 输出: {"user" : {"status" : {"stat1" : "54", "stats2" : "87"}}} 我从这个 API 创建了一个简单的 JSONObject
我正在寻找一种方法来比较两个 JSONObjects 除了使用 JSONObject.toString().equals(JSONObject.toString()) 并遇到了这种类似的方法,但它的比
我在读取和写入 Json 文件时遇到问题。我想将一些内容附加到 json 文件中,但它无法正常工作:它只是放入一个新的 json 对象,而没有使用 ',' 将其与前一个对象分开。我在每个网站上到处搜索
我有 JSON 文件:“D://test.json”,它遵循 MongoDB 格式: { "key":"value" } { "embedded": { "key2": "value2" } } {
我想要一个像这样的 json 文件: { "resu0": {"item":"value"} "resu1": {"item":"value"} "resu2": {"item":"value"} }
我有一个像这样的 JSON { "result": { "issue_date": "xx-yy-zzzz", "father/husband": "TEST",
我想发送json,里面有另一个json对象,像这样 { "key1": "value1", "key2": "valu2", "content": {
private JSONObject insertJSONintoJSON(JSONObject newJSON, JSONObject parent) { Object[] a = pare
常规更新:我之前询问是否可以按原样获取 JSONObject 的值,以便稍后将其与覆盖的版本进行比较。我的第一次尝试是: JSONObject oldObj = new JSONObject(); p
我正在尝试创建一个具有特定键和空值的 JSONObject。但该值应该是 JSONObject 而不是 JSONArray。 我已经使用 localJson.append(key, JSONObjec
我得到的所有资源都是从 JSONArray 获取 JSONObject。但在这种情况下,我想获得“temp”的温度。我不认为它是数组的一部分。我怎样才能得到它? { "coord":{
我正在尝试解析 JSONObject 以获取 JSON 数组的数据。 但问题是 JSONParser 是 org.json.simple.JSONParser 中的一个类,而 JSONObject 位
我发现 variable instanceof JSONObject 有奇怪的行为。我有以下代码: Object value; try { value = customer.get(key);
JsonObject 和 JSONObject 有什么区别? 当我们使用 put、add 和 addproperty 方法时,我有点混淆 JsonObject 和 JSONObject。 提前致谢。
我想从 Another JSONObject 中的一个 JSONObject 中检索数据谁能告诉我如何取回它们 我的JSONObject是这样的 {"udetail":{"ID":238597,"Re
你是否注意到,如果你使用 put() 将一个 JSONObject 放入另一个 JSONObject,容器 JSONObject 将不会存储对第一个 JSONObject 的引用,而是一个新的 JSO
我的应用程序构建得很好,但是当我运行它时,当以下行尝试执行时,我不断收到异常: JSONObject json = new JSONObject(); 我收到以下异常: 异常 javax.servle
我是一名优秀的程序员,十分优秀!