[html]
<uses-permission android:name="android.permission.INTERNET" /> 由于访问网络图片是比较耗时的操作,所以在正式项目中使用异步加载图片,效果会更好。运行效果:
2.HttpClient
下面使用HttpClient获取网页内容:
[html]
public class HttpClientActivity extends Activity { private ImageView imageview; private TextView text; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.simple2); imageview=(ImageView) this.findViewById(R.id.imageView2); text=(TextView) this.findViewById(R.id.textView2); HttpGet httpGet=new HttpGet("http://cloud.csdn.net/a/20120209/311628.html"); HttpClient httpClient=new DefaultHttpClient(); try { //得到HttpResponse对象 HttpResponse httpResponse=httpClient.execute(httpGet); //HttpResponse的返回结果是不是成功 if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){ //得到返回数据的字符串 String dataImageStr=EntityUtils.toString(httpResponse.getEntity()); text.setText(dataImageStr); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 运行效果:这样就成功加载了网页内容。