Android中基於Http協議的網路通訊的兩種方法Get和Post
阿新 • • 發佈:2019-02-05
廢話就不多說了直接上程式碼,很多的解釋就直接寫在程式碼中了!
第一種方法:Get方法:
第二種方法:post方法:public class Get { private HttpResponse httpResponse = null; private HttpEntity httpEntity = null; private String url; Handler handler; //建構函式 用來 給UI介面傳遞引數,例如這個就是傳遞了URL和handler,當在IU介面進行操作 接收資料的時候,就會用到這些引數。 public Get(String url, Handler handler) { // TODO Auto-generated constructor stub this.url = url; this.handler = handler; } //這個網路通訊基於android的Http協議之上的。 public void Httpget() { //為什麼 要開闢一個新的執行緒呢,其實這個呢,我也不太清楚,只知道資料上說:android2.3還是哪個版本之後,說IU介面不能和這個使用同一個執行緒,否則會報錯,程式停止執行 new Thread() { @Override public void run() { // 先生成一個請求物件,String url = baseURL + "?" + "UserOID=" + str;地址中包含了引數。 HttpGet httpget = new HttpGet(url); // 生成一個客戶端物件 HttpClient httpClient = new DefaultHttpClient(); // 使用Http客戶端傳送請求物件 InputStream inputStream = null; try { httpResponse = httpClient.execute(httpget); httpEntity = httpResponse.getEntity(); inputStream = httpEntity.getContent(); // 當我的請求傳送成功時,將接受的資料傳進來 if (httpResponse.getStatusLine().getStatusCode() == 200) { BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream)); //定義的一個字串物件,用來儲存從伺服器上接受到的字元! String result = ""; //line是一個或者一行一行的讀取,最後儲存在result中! String line = ""; while ((line = reader.readLine()) != null) { result = result + line; Message msg = new Message(); msg.obj = result; handler.sendMessage(msg); } } } catch (Exception e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } }.start(); } }
//post方式向伺服器傳送資料 public class Post { protected static final String DataID = null; protected static final String DeviceEntityAID = null; protected static final String Value = null; // HttpResponse/HttpEntity 是介面 private HttpResponse httpResponse = null; private HttpEntity httpEntity = null; private String str; Handler handler; //建立建構函式傳入引數 str,handler public Post(String str,Handler handler) { this.str = str; this.handler=handler; } public void SendMssage() { new Thread() { @Override public void run() { try { // 生成一個客戶端物件httpClient HttpClient httpClient = new DefaultHttpClient(); // 生成一個請求物件,"http://192.168.11.5:3021/serviceUp/cmd/sendCMDDispatcher"就是你的伺服器的地址,新增你自己的伺服器的地址就ok了 HttpPost httpPost = new HttpPost( "http://192.168.11.5:3021/serviceUp/cmd/sendCMDDispatcher"); // 建立一個NameValuePair陣列,用於儲存欲傳送的引數,引數放在了陣列中。 List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); //list.add(new BasicNameValuePair("name", name)); //list.add(new BasicNameValuePair("pwd", pwd)); // 新增引數,這些引數根據自己伺服器上的資料格式設定,主要是看你的的伺服器上的資料格式的 str = "["; str += "{"; str += "\"DeviceEntityAID\":" + "\"" + DeviceEntityAID + "\""; str += ","; str += "\"DataID\"" + ":" + "\"" + DataID + "\""; str += ","; str += "\"Value\"" + ":" + "\"" + Value + "\""; str += "}"; str += "]"; // String // abc="[{\"DeviceEntityAID\":\"02000200\",\"DataID\":\"02060000\",\"Value\":\"0003\"}]"; //設定引數 cmdContent是伺服器端的key值,str為自己要輸入的引數,其字元格式由伺服器端後決定, //將傳送的引數通過add的方法新增到BasicNameValuePair類的物件中。 postParameters .add(new BasicNameValuePair("cmdContent", str)); // 設定編碼,這個一般情況下都是預設的UTF-8編碼格式 httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8")); // 傳送Post,並返回一個httpResponse物件 httpResponse = httpClient.execute(httpPost); httpEntity = httpResponse.getEntity(); //HttpPost連結解析xml,response.getStatusLine().getStatusCode()==200連結成功,==500失敗。 if (httpResponse.getStatusLine().getStatusCode() == 200) { //讀取字串 BufferedReader reader = new BufferedReader( new InputStreamReader(httpEntity.getContent())); String result = ""; String line = ""; while ((line = reader.readLine()) != null) { result = line; //通過Message的類建立一個msg物件 Message msg = new Message(); //使用其物件msg呼叫message中的obj方法 msg.obj = result; handler.sendMessage(msg); } } } catch (Exception e) { e.printStackTrace(); } finally { try { httpEntity.getContent().close(); } catch (Exception e) { e.printStackTrace(); } } } }.start(); } }
兩者之間的不同:由此我們可知,HttpGet和HttpPost的區別在於前者是將引數在地址中傳遞,後者是將引數用List傳遞。