標籤:
一,通過Apache介面訪問網路所必須的類
HttpClient
概述
---HttpClient 是 Apache Jakarta Common 下的子項目,可以用來提供高效的、最新的、功能豐富的支援 HTTP 協議的用戶端編程工具包,並且它支援 HTTP 協議最新的版本和建議。(摘自百度百科)
成員變數
構造方法
---DefaultHttpClient()
普通方法
---execute()//發送請求,需要一個表示要求方法的對象作為參數,返回一個封裝了響應資訊的HttpResponse對象
靜態方法
HttpGet
概述
---如果HttpClient發送請求時使用該對象作為參數則表示發送的是GET請求,該請求一般用於擷取/查詢資源資訊
---GET請求的資料會附在URL之後(就是把資料放置在HTTP協議頭中),以?分割URL和傳輸資料,參數之間以&相連,如:login.action?name=hyddd&password=idontknow&verify=%E4%BD%A0%E5%A5%BD。如果資料是英文字母/數字,原樣發送,如果是空格,轉換為+,如果是中文/其他字元,則直接把字串用BASE64加密,得出如:%E4%BD%A0%E5%A5%BD,其中%XX中的XX為該符號以16進位表示的ASCII。
成員變數
構造方法
---HttpGet()//構造一個GET請求,需要一個WEB地址作為參數
普通方法
靜態方法
HttpResponse
概述
---該類封裝了發起Http請求後返回的資訊
成員變數
構造方法
---HttpClient.execute()//用戶端發送請求後返回該對象執行個體
普通方法
---getStatusLine()//獲得一個封裝了"伺服器響應請求的狀態資訊"的對象
---getStatusCode()//通過上述對象獲得“伺服器的響應碼”
---getEntity()//獲得伺服器響應實體
靜態方法
HttpPost
概述
---如果HttpClient發送請求時使用該對象作為參數則表示發送的是POST請求,該請求一般用於更新資源資訊
---POST把提交的資料放置在HTTP包的包體中,也就是HttpEntity中
成員變數
構造方法
---HttpPost()//構造一個GET請求,需要一個WEB地址作為參數
普通方法
---setEntity(Params)//佈建要求參數
靜態方法
NameValuePair
概述
---發送HttpPost請求需要通過該對象封裝請求參數,一個該對象代表一個鍵值對形式的參數
成員變數
構造方法
------new BasicNameValuePair(key,value)
普通方法
靜態方法
HttpEntity
概述
---該類用來封裝用戶端與服務端通訊過程中的資料流
成員變數
構造方法
---HttpResponse.getEntity()
---new UrlEncodedFormEntity(params,"utf-8");//封裝Post請求的參數和字元集編碼,參1是持有NameValuePair參數對象的容器,參二是字元集編碼
普通方法
---getContent()//獲得伺服器返回的資料流
靜態方法
注
---EntityUtils.toString(HttpEntity)//該方法可直接將一個HttpEntity對象中封裝的資料流解析成字串資料
總結
---GET
----1,首先我們需要一個用戶端用來串連伺服器,new DefaultHttpClient(),2,然後需要告訴用戶端我們要訪問伺服器裡的哪個檔案並且以哪種方式去訪問,new HttpGet(URL)。3,現在就可以串連並訪問伺服器,HttpClient.execute(HttpGet),伺服器會返回它的響應資訊,要求方法會自動將這些資訊封裝到HttpResponse中並返回。資料已經拿到了,至此就沒有伺服器和用戶端卵事了。
---POST
----同上,HttpGet自然是要改成HttpPost。如果你需要向伺服器傳遞參數的話:首先構造封裝了參數的對象,new BasicNameValuePair(key,value),一個對象只能儲存一組鍵值對。然後將該對象儲存到容器中,new ArrayList<NameValuePair>().add(參數對象),再將容器封裝到HttpEntity中,new new UrlEncodedFormEntity(容器物件,"utf-8")。最後,HttpPost出場包了她們,HttpPost.setEntity(HttpEntity)。
二,通過Apache介面訪問網路執行個體
1,首先在你的伺服器裡放個用來被訪問的web檔案
<%@page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>
Http Test
</title>
</head>
<body>
<%
String type = request.getParameter("parameter");
String result = new String(type.getBytes("iso-8859-1"),"utf-8");
out.println("<h1>" + result + "</h1>");
%>
</body>
</html>
2,Android端代碼
public class MainActivity extends Activity {
Handler han=new Handler(){
public void handleMessage(android.os.Message msg) {
switch(msg.what){
case 1:
tv.setText(msg.obj.toString());
break;
case 2:
tv.setText(msg.obj.toString());
break;
}
}
};
TextView tv;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.t1);
et=(EditText) findViewById(R.id.et);
}
public void get(View view){
String param=et.getText().toString();
if(TextUtils.isEmpty(param)){
param="空";
}
new th1(param);
}
public void post(View view){
String param=et.getText().toString();
if(TextUtils.isEmpty(param)){
param="空";
}
new th2(param);
}
class th1 extends Thread{
String params="";
public th1(String pa){
this.params=pa;
this.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
HttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet("http://11.67.0.58:8080/nihao/map.jsp?parameter="+params);
try {
//發送請求並擷取響應資訊
HttpResponse request=client.execute(get);
//判斷請求是否成功
if(request.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
//請求成功後擷取響應實體
HttpEntity entity=request.getEntity();
//解析伺服器返回的資料流
InputStream is=entity.getContent();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
StringBuffer sb=new StringBuffer();
String dataLine="";
while((dataLine=br.readLine())!=null){
sb.append(dataLine);
}
Message msg=han.obtainMessage();
msg.what=1;
msg.obj=sb.toString();
han.sendMessage(msg);
br.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
}
class th2 extends Thread{
String param="";
public th2(String pa){
param=pa;
this.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost("http://11.67.0.58:8080/nihao/map.jsp");
//構造參數對象
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("parameter",param));
HttpEntity entity;
try {
entity = new UrlEncodedFormEntity(params,"utf-8");
post.setEntity(entity);
HttpResponse response=client.execute(post);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
InputStream is=entity.getContent();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
StringBuffer sb=new StringBuffer();
String dataLine="";
while((dataLine=br.readLine())!=null){
sb.append(dataLine);
}
Message msg=han.obtainMessage();
msg.what=2;
msg.obj=sb.toString();
han.sendMessage(msg);
br.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Android網路編程之Apache介面