android 处理http例子


package com.example.cooler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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 android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class HttpActivity extends Activity {

	private TextView tv1 = null;
	private Button btn_http = null;
	private HttpResponse httpResponse = null;
	private HttpEntity httpEntity = null;
	private Handler handler = null;
	private String content = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.active_http);

		// 创建属于主线程的handler
		handler = new Handler();

		btn_http = (Button) findViewById(R.id.btn_http);
		btn_http.setOnClickListener(new HttpListener());

	}

	class HttpListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			tv1 = (TextView) findViewById(R.id.editTextHttp);
			tv1.setText("start....");
			// String result = tv1.getText().toString();
			new Thread() {
				public void run() {
					// 生成请求对象
					HttpGet httpgGet = new HttpGet("http://www.baidu.com");
					// 生成httpclient对象
					HttpClient httpClient = new DefaultHttpClient();
					InputStream inputStream = null;
					try {
						httpResponse = httpClient.execute(httpgGet);
						httpEntity = httpResponse.getEntity();
						inputStream = httpEntity.getContent();
						BufferedReader reader = new BufferedReader(
								new InputStreamReader(inputStream));
						String result = "";
						String line = "";
						while ((line = reader.readLine()) != null) {
							result = result + line;
						}
						content = result;
						handler.post(runnableUi);
						System.out.println(result);
					} catch (ClientProtocolException e) {
						// TODO Auto-generated catch block
						// e.printStackTrace();
						Log.v("client ", "11");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						// e.printStackTrace();
						Log.v("IO ", "22");
					} finally {
						try {
							inputStream.close();
						} catch (Exception e3) {
							Log.v("e3  ", "33");
							// e3.printStackTrace();
							// TODO: handle exception
						}
					}
				}
			}.start();
		}
	}

	// 构建Runnable对象,在runnable中更新界面
	Runnable runnableUi = new Runnable() {
		@Override
		public void run() {
			// 更新界面
			tv1.setText("the Content is:" + content);
		}

	};
}




<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#EEE685" >

    <LinearLayout
        android:id="@+id/linerLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#E0FFFF"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_http"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/btn_http" />

        <EditText
            android:id="@+id/editTextHttp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textMultiLine" >

            <requestFocus />
        </EditText>
    </LinearLayout>

</FrameLayout>

你可能感兴趣的:(android)