提示 需要在AndroidManifest.xml添加访问网络的权限
<uses-permission android:name="android.permission.INTERNET"/>
以下为代码
package com.example.gethtmlcontant;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
private TextView tv_html_contant;
private static final String DEFAULT_URI = "http://xxxxxxxx.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_html_contant = (TextView) findViewById(R.id.tv_html_contant);
}
/**
* 设置内容到文本框上
*
* @param s
*/
private void setContant(final String s) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (tv_html_contant != null) {
tv_html_contant.setText(s);
}
}
});
}
/**
* 清空文本框
*
* @param s
*/
public void clearText(View v) {
if (tv_html_contant != null) {
tv_html_contant.setText("");
}
}
/**
* get 方式
*
* @param v
*/
public void setDoGet(View v) {
new Thread() {
public void run() {
HttpClient mHttpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(DEFAULT_URI);
try {
HttpResponse response = mHttpClient.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 如果请求成功,则设置到界面上
setContant(inStream2String(response.getEntity()
.getContent()));
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
/**
* post 方式
*
* @param v
*/
public void setDoPost(View v) {
new Thread() {
public void run() {
HttpClient mHttpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(DEFAULT_URI);
//添加参数 这里不需要
// List <NameValuePair> params = new ArrayList<NameValuePair>();
// params.add(new BasicNameValuePair("key", "values"));
// try {
// httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
// } catch (UnsupportedEncodingException e1) {
// e1.printStackTrace();
// }
try {
HttpResponse response = mHttpClient.execute(httpPost);
System.out.println( response.getStatusLine().getStatusCode()+"");
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 如果请求成功,则设置到界面上
setContant(inStream2String(response.getEntity()
.getContent()));
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
/**
* HttpURLConnection 方式
*
* @param v
*/
public void setHttpConnection(View v) {
new Thread() {
public void run() {
HttpURLConnection connection = null;
try {
URL url = new URL(DEFAULT_URI);
connection = (HttpURLConnection) url.openConnection();
// 设置请求方式
connection.setRequestMethod("GET");
// 设置请求超时的时间
connection.setConnectTimeout(4000);
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
// 如果请求成功,则设置到界面上
setContant(inStream2String(connection.getInputStream()));
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
/**
* 从输入流中读取字串
*
* @param is
* @return
*/
private String inStream2String(InputStream is) {
String temp = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int length = -1;
try {
while ((length = is.read(b)) != -1) {
bos.write(b, 0, length);
}
temp = new String(bos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (bos != null) {
try {
bos.close();
bos = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return temp;
}
}
布局文件代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setDoGet"
android:text="Get"
android:textSize="10sp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setDoPost"
android:text="Post"
android:textSize="10sp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setHttpConnection"
android:text="HttpConnection"
android:textSize="10sp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clearText"
android:text="CleanText"
android:textSize="10sp" />
</LinearLayout>
<TextView
android:id="@+id/tv_html_contant"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" />
</LinearLayout>