【Android】Web开发之通过Apache接口处理Http请求
package com.app.myweb;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.NameValuePair;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 org.json.JSONArray;import org.json.JSONObject;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;/** * 范例:通过Apache接口处理Http请求 */public class ApacheHttp_JSP extends Activity implements OnClickListener {private TextView textView1, textView2, textView3;private Button button1, button2;private StringBuffer sb;private String result;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.http_jsp);setUI(); setAction();}public void setUI() {/* * textView1 = (TextView) findViewById(R.id.textView1); * textView1.setText("利用Java标准接口java.net.*类实现读取指定url内容"); */textView3 = (TextView) findViewById(R.id.textView3);button1 = (Button) findViewById(R.id.button1);button2 = (Button) findViewById(R.id.button2);button1.setText("Apache接口:发送GET请求");button2.setText("Apache接口:发送POST请求");}public void setAction() {findViewById(R.id.button1).setOnClickListener(this);findViewById(R.id.button2).setOnClickListener(this);}/** 发送GET请求并获取服务器端返回值 */public String handleGet(String strUrl) {StringBuffer buffer = null;HttpGet request = new HttpGet(strUrl);//实例化一个HttpGet请求(指定URL)DefaultHttpClient client = new DefaultHttpClient();//实例化一个客户端try {HttpResponse response = client.execute(request);//调用客户端的execute(request)执行请求//获取服务器端返回的状态码是否等于200if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {result = EntityUtils.toString(response.getEntity());//调用getEntity获取返回值,需通过EntityUtils把实体转成String} else {result = response.getStatusLine().toString();}} catch (Exception e) { }return result;}/** 携带一个params数据发送Post请求到指Url */public String handlePost(String strUrl, List<NameValuePair> params) {HttpPost request = new HttpPost(strUrl);//建立HTTPPost对象try {//设置该请求要携带的参数request.setEntity(new UrlEncodedFormEntity(params, "GBK"));HttpResponse response = new DefaultHttpClient().execute(request);if (response.getStatusLine().getStatusCode() == 200) {result = EntityUtils.toString(response.getEntity());} else {result = response.getStatusLine().toString();}} catch (Exception e) {e.printStackTrace();}return result;}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.button1:textView3.setText(handleGet("http://10.0.2.2:8888/android/1.jsp"));break;case R.id.button2:List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("username", "tom"));params.add(new BasicNameValuePair("password", "1"));textView3.setText(handlePost("http://10.0.2.2:8888/android/2.jsp", params));break;default:break;}}}