1. 程式人生 > >HttpClient gzip壓縮post提交資料

HttpClient gzip壓縮post提交資料

ButtongzipButton = (Button) findViewById(R.id.button1);//按鈕1-gzip壓縮

ButtonnogzipButton = (Button) findViewById(R.id.button2);//按鈕2-正常傳送

        gzipButton.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

finalStringmessageString = "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";

finalStringurlString = “http://xxxxxxxx.com”//url

finalStringmessageString2 = 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();

       }

HttpResponsehttpResponse = httpClient.execute(httpPost);

if (httpResponse.getStatusLine().getStatusCode() == 200) {

//***

        }

    }

//正常傳送-非gzip壓縮

        nogzipButton.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

finalStringmessageString = "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";

finalStringurlString = “http://xxxxxxxx.com”;//url

finalStringmessageString2 = 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();

        }

HttpResponsehttpResponse = httpClient.execute(httpPost);

if (httpResponse.getStatusLine().getStatusCode() == 200) {

//****

        }

    }

ps:上述程式碼比較冗餘,可以進一步優化。