Android中运用httpclient等小结

Android中使用httpclient等小结
android中也可以其实象普通JAVA那样,去打开远程的网页和用apache的httpclient
去获得页面内容.下面是代码,两种方法都有了:

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidTelenet extends Activity {
    /** Called when the activity is first created. */
HttpUriRequest request=null;
HttpResponse resp=null;
InputStream is=null;
DefaultHttpClient client=new DefaultHttpClient();

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
/* We will show the data we read in a TextView. */
TextView tv = new TextView(this);
/* Will be filled and displayed later. */
StringBuffer myString = new StringBuffer();
String tips=null;
try {
/* Define the URL we want to load data from. */
// URL myURL = new URL(
// "http://10.0.2.2:8085/test/Index.action");
/* Open a connection to that URL. */
// URLConnection ucon = myURL.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
// InputStream is = ucon.getInputStream();

//BufferedInputStream bis = new BufferedInputStream(is);

//if (bis.read()>0)
//{
// tips="能连上网页";
//}
//else
//{
// tips="不能连接网页";
//}
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
// ByteArrayBuffer baf = new ByteArrayBuffer(50);
//int current = 0;
//while ((current = bis.read()) != -1) {
//baf.append((byte) current);
// }
/* Convert the Bytes read to a String. */
//myString = new String(baf.toByteArray());
request=new HttpGet("http://10.0.2.2:8085/test/Index.action");
resp=client.execute(request);
  HttpEntity entity = resp.getEntity();  
              InputStream is = entity.getContent();  
              BufferedReader br = new BufferedReader(  
                      new InputStreamReader(is,"GB2312"));  
              String data = "";  

              while ((data = br.readLine()) != null) {  
                  myString.append(data);  
              }  
              String result = myString.toString(); 
System.out.println("result is"+result);

System.out.println("mystring"+myString);
if (myString.indexOf("对不起,操作错误或系统维护中")>0)
{
tips="TOMCAT启动,数据库等其他异常";
}
else
{
tips="正常,无异常信息";
}

} catch (Exception e) {
/* On any Error we want to display it. */
tips= e.getMessage();
}
/* Show the String on the GUI. */
tv.setText(tips);
this.setContentView(tv);
}

    }