httpClient + AsyncTask

package lee.HttpClientDemo;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	private Button but;
	private TextView context;
	String str="abc" ;
	private ProgressDialog progressDlg; 
	
	Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			if (msg.what==1) {
				progressDialog.dismiss();
				System.out.println(str);
				context.setText(str);
			}

			super.handleMessage(msg);
		}
		
	};
	
	
	private ProgressDialog progressDialog = null;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context=(TextView) findViewById(R.id.context);
        but=(Button) findViewById(R.id.but);
        but.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				progressDialog = ProgressDialog.show(MainActivity.this,"连接","正在连接中", true);
				HttpClientTask clientTask=new HttpClientTask();
				clientTask.execute();
				
				
				
//				new Thread(){
//					public void run(){
//						try {
//							String path = "http://google.com";
//							HttpGet httpGet = new HttpGet(path);
//							HttpParams httpParameters = new BasicHttpParams(); 
//							HttpConnectionParams.setConnectionTimeout(httpParameters, 11000);  
//							HttpConnectionParams.setSoTimeout(httpParameters, 11000); 
//							DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); 
//							HttpResponse response;
//							response = httpClient.execute(httpGet);
//							str = EntityUtils.toString(response.getEntity());
//							
//							 handler.sendEmptyMessage(1);
//						} catch (Exception e) {
//							 handler.sendEmptyMessage(1);
//							e.printStackTrace();
//						} 
//					}
//				}.start();
			}
		});
    }
	
	public class HttpClientTask extends AsyncTask{

		@Override
		protected Object doInBackground(Object... params) {
			
			try {
				String path = "http://google.com";
				HttpGet httpGet = new HttpGet(path);
				HttpParams httpParameters = new BasicHttpParams(); 
				HttpConnectionParams.setConnectionTimeout(httpParameters, 1000);  
				HttpConnectionParams.setSoTimeout(httpParameters, 1000); 
				DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); 
				HttpResponse response;
				response = httpClient.execute(httpGet);
				InputStream is = response.getEntity().getContent();	//获取输入流
				str = EntityUtils.toString(response.getEntity());	//获取字符串
			
			}catch (Exception e) {
				System.out.println(e.toString());
			}
			return str;
		}

		@Override
		protected void onCancelled() {
			// TODO Auto-generated method stub
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(Object result) {
			progressDlg.dismiss();
			context.setText(result.toString());
			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute() {
			  progressDlg = new ProgressDialog(MainActivity.this); 
			  progressDlg.setMessage("请稍等..."); 
			  progressDlg.setCancelable(false); 
			  progressDlg.show(); 
			super.onPreExecute();
		}

		@Override
		protected void onProgressUpdate(Object... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
		}
	}
}

 

你可能感兴趣的:(apache,thread,android,OS,Google)