测试 android gzip
package com.skylun.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.crazyit.auction.client.util.HttpUtil;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TestHttpActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */

private TextView textView;
private Button btn;
private Handler handler;
private Thread httpThread;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textView = (TextView)this.findViewById(R.id.textView);
        
        btn = (Button)this.findViewById(R.id.btn);
        btn.setOnClickListener(this);
        
        handler = new Handler(){

@Override
public void handleMessage(Message msg) {

}

};
    }

@Override
public void onClick(View v) {
httpThread = new Thread() {

@Override
public void run() {
try {
Log.v("skylun","----------start-------------");
Message msg = new Message();

HttpClient httpClient = new DefaultHttpClient();
// 创建HttpGet对象。
HttpGet get = new HttpGet(HttpUtil.BASE_URL);
get.addHeader("Accept-Encoding", "gzip");
long startTime = System.currentTimeMillis();

// 发送GET请求
HttpResponse httpResponse = httpClient.execute(get);

Header[] headerArr = httpResponse.getAllHeaders();
for (Header h:headerArr) {
Log.v("skylun", h.getName()+"--------"+h.getValue());
}

if (httpResponse.getStatusLine().getStatusCode() == 200)
{
HttpEntity httpEntity = httpResponse.getEntity();
    Log.v("skylun","----------resut encoding value-------------"+httpEntity.getContentEncoding());
    if (httpEntity.getContentEncoding()!=null &&httpEntity.getContentEncoding().getValue()!=null&&
     httpEntity.getContentEncoding().getValue().contains("gzip")) {
     InputStream is= httpEntity.getContent();
        is= new GZIPInputStream(is);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        StringBuffer sb = new StringBuffer();
        while((line = br.readLine())!=null) {
         sb.append(line);
        }
        Log.v("skylun","----------gzip result str-------------"+sb.toString());
    } else {
     // 获取服务器响应字符串
String result = EntityUtils.toString(httpEntity);
Log.v("skylun","----------result-------------"+result);
//msg.obj = result;
    }

}
long endTime = System.currentTimeMillis();
Log.v("skylun","----------请求时间-------------"+(endTime-startTime)+"毫秒");
handler.sendMessage(msg);
Log.v("skylun","----------end-------------");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
httpThread.start();
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="提交" />

</LinearLayout>
lunzi   2012-05-24 00:11:34 评论:0   阅读:1178   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog