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 = "";@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.active_http);// 创建属于主线程的handlerhandler = new Handler();btn_http = (Button) findViewById(R.id.btn_http);btn_http.setOnClickListener(new HttpListener());}class HttpListener implements OnClickListener {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtv1 = (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() {@Overridepublic 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>

更多相关文章

  1. 类和 Json对象
  2. android视频录制(调用系统视频录制),生成缩略图
  3. Android获取GPS坐标:
  4. Android(安卓)随机生成验证码的bitmap
  5. android发送json并解析返回json
  6. Android(安卓)封装http请求的工具类
  7. android 之simpleAdapter详解
  8. Android(安卓)-- 生成二维码
  9. Android轻量级JSON操作类

随机推荐

  1. selector属性介绍
  2. Android(安卓)Studio 3.0开始android Dev
  3. 初学Android时,运行SDK的例子出现警告
  4. Android(安卓)浏览器的开发实例分享
  5. Android设置Settings实现:PreferenceActiv
  6. Google android初级开发之 : android 编
  7. Android学习笔记之mainfest文件中android
  8. Android使用gradle生成maven库,上传githu
  9. android jni 程序框架搭建
  10. TableLayout