package com.gg.test;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
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 android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button bt_start;
private TextView tv_textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
// 找对象
bt_start = (Button) findViewById(R.id.bt_start);
tv_textview = (TextView) findViewById(R.id.tv_textview);
bt_start.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_start:
MyAsy myAsy = new MyAsy();
myAsy.execute("https://www.baidu.com/?tn=92760364_hao_pg");
break;
}
}
/**
* 异步,内部类
*
* @author gumenghao
*
*/
class MyAsy extends AsyncTask<String, String, String> {
private String str_xml;
@Override
protected String doInBackground(String... params) {
String str_internet = params[0];
HttpGet htptGet = new HttpGet(str_internet);
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse httpResponse = httpClient.execute(htptGet);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
str_xml = EntityUtils.toString(httpResponse.getEntity(),
"UTF-8");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block1
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str_xml;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
tv_textview.setText(str_xml);
}
}
}