Button gzipButton = (Button) findViewById(R.id.button1);//按钮1-gzip压缩
Button nogzipButton = (Button) findViewById(R.id.button2);//按钮2-正常发送
gzipButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String messageString = "1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890";
final String urlString = “http://xxxxxxxx.com”//url
final String messageString2 = messageString+messageString+messageString+messageString+messageString+messageString;//拼接一个较长的字符串,模拟较大的数据。
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", "123");
map.put("age", "30");
map.put("sssdd", "662");
map.put("erwef", "657416");
map.put("kkk", "151");
map.put("messageString", messageString2);
final String mapString = map.toString();
//开辟线程
Thread thread=new Thread(new Runnable()
{
@Override
public void run()
{
try {
//发送
sendHttp(urlString, mapString);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
thread.start();
}
});
//gzip 压缩发送
public void sendHttp(String url, String message) throws ClientProtocolException, IOException {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Encoding", "gzip");
try {
ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
originalContent.write(message.getBytes(Charset.forName("UTF-8")));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
originalContent.writeTo(gzipOut);
gzipOut.finish();
httpPost.setEntity(new ByteArrayEntity(baos.toByteArray()));
} catch (Exception e) {
e.printStackTrace();
}
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
//***
}
}
//正常发送-非gzip压缩
nogzipButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String messageString = "1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890";
final String urlString = “http://xxxxxxxx.com”;//url
final String messageString2 = messageString+messageString+messageString+messageString+messageString+messageString;
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", "sse");
map.put("age", "30");
map.put("qweqw", "dad");
map.put("wdqawd", "ddd");
map.put("kkk", "151");
map.put("messageString", messageString2);
final String mapString = map.toString();
Thread thread=new Thread(new Runnable()
{
@Override
public void run()
{
try {
sendHttp2(urlString, mapString);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
thread.start();
}
});
public void sendHttp2(String url, String message) throws ClientProtocolException, IOException {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new StringEntity(message));
} catch (Exception e) {
e.printStackTrace();
}
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
//****
}
}
ps:上述代码比较冗余,可以进一步优化。