主布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是一个HTTP通信方式的示例"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="通过get方式获取数据"
android:id="@+id/bt_get"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="通过post方式获取数据"
android:id="@+id/bt_post"/>
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tv_content"/>
</LinearLayout>
主活动类HttpMainActivity.java:
package com.example.ch10;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.example.baseexample.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class HttpMainActivity extends Activity {
private TextView tv_content;
private Button bt_get,bt_post;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ch10_httpmain);
tv_content = (TextView)findViewById(R.id.tv_content);
bt_get = (Button)findViewById(R.id.bt_get);
bt_post = (Button)findViewById(R.id.bt_post);
bt_get.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
try{
final String httpUrl = "http://192.168.1.100:8081";
HttpGet httpRequest = new HttpGet(httpUrl);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpRequest);
if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
String strResult = EntityUtils.toString(httpResponse.getEntity());
tv_content.setText(strResult);
}else{
tv_content.setText("请求错误");
}
}catch(Exception e){
Toast.makeText(HttpMainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
bt_post.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
try{
final String httpUrl = "http://10.0.2.2:8080/WebDemo/UI/httptest.jsp";
HttpPost httpRequest = new HttpPost(httpUrl);
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("par", "Post Type:abcdef"));
HttpEntity entity = new UrlEncodedFormEntity(param,"utf-8");
httpRequest.setEntity(entity);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpRequest);
if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
String strResult = EntityUtils.toString(httpResponse.getEntity());
tv_content.setText(strResult);
}else{
tv_content.setText("请求错误");
}
}catch(Exception e){
Toast.makeText(HttpMainActivity.this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}
});
}
}
添加访问Internet的权限:
<uses-permission android:name="android.permission.INTERNET"/>