package com.example.demorequest;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;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 android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity { private Button getButton = null; private Button postButton = null; private EditText strView = null; private String baseUrl = "http://www.baidu.com/s?"; private HttpResponse httpResponse = null;//響應對象 private HttpEntity httpEntity = null;//取出響應內容的訊息對象 InputStream inputStream = null;//輸入資料流對象 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); strView = (EditText) findViewById(R.id.strView); getButton = (Button) findViewById(R.id.getButton); postButton = (Button) findViewById(R.id.postButton); //get方法發送請求 getButton.setOnClickListener(new OnClickListener(){ public void onClick(View arg0) { String str = strView.getText().toString(); String url = baseUrl + "?wd=" + str; //產生一個請求對象 HttpGet httpGet = new HttpGet(url); //產生一個http用戶端對象 HttpClient httpClient = new DefaultHttpClient(); //發送請求 try { httpResponse = httpClient.execute(httpGet);//接收響應 httpEntity = httpResponse.getEntity();//取出響應 //用戶端收到響應的資訊流 inputStream = httpEntity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String result = ""; String line = ""; while((line = reader.readLine()) != null){ result = result + line; } System.out.println(result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{//最後一定要關閉輸入資料流 try{ inputStream.close(); }catch(Exception e){ e.printStackTrace(); } } } }); //post方法發送請求 postButton.setOnClickListener(new OnClickListener(){ public void onClick(View arg0) { // TODO Auto-generated method stub String str = strView.getText().toString();//參數 NameValuePair nameValuePair = new BasicNameValuePair("content", str);//索引值對 //然後將索引值對放到列表裡(類似於形成數組) //List是一個介面,而ListArray是一個類。ListArray繼承並實現了List。所以List不能被構造,但可以向上面那樣為List建立一個引用,而ListArray就可以被構造。 //List list = new ArrayList();這句建立了一個ArrayList的對象後把上溯到了List。此時它是一個List對象了 //而ArrayList list=new ArrayList();建立一對象則保留了ArrayList的所有屬性。 //為什麼一般都使用 List list = new ArrayList() ,而不用 ArrayList alist = new ArrayList()呢? 問題就在於List有多個實作類別,如 LinkedList或者Vector等等,現在你用的是ArrayList,也許哪一天你需要換成其它的實作類別呢?,這時你只要改變這一行就行了:List list = new LinkedList(); 其它使用了list地方的代碼根本不需要改動。 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(nameValuePair);//將索引值對放入到列表中 try { HttpEntity requestHttpEntity = new UrlEncodedFormEntity(nameValuePairs);//對參數進行編碼操作 //產生一個post請求對象 HttpPost httpPost = new HttpPost(baseUrl); httpPost.setEntity(requestHttpEntity); //產生一個http用戶端對象 HttpClient httpClient = new DefaultHttpClient();//發送請求 try { httpResponse = httpClient.execute(httpPost);//接收響應 httpEntity = httpResponse.getEntity();//取出響應 //用戶端收到響應的資訊流 inputStream = httpEntity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String result = ""; String line = ""; while((line = reader.readLine()) != null){ result = result + line; } System.out.println(result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{//最後一定要關閉輸入資料流 try{ inputStream.close(); }catch(Exception e){ e.printStackTrace(); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); }}
<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:orientation="vertical" > <EditText android:id="@+id/strView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/getButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="使用get方法發送請求" /> <Button android:id="@+id/postButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="使用post方法發送請求" /></LinearLayout>