在Android開發過程當中,咱們常常會遇到調用後臺服務的狀況。目前我手上進行的項目,後臺服務基本上基於.NET環境開發的,開發語言是C#。服務通常是供前端AJAX請求調用。前端
而在Android中,咱們也能夠經過HTTP請求訪問相似的服務,主要請求方法都是GET或者POST,而且POST請求包含了冗長的參數。所以我本身封裝了GET和POST的HTTP請求的方法,POST請求參數經過輸入流寫入,方法使用的是HttpURLConnection類。緩存
話很少說,我直接貼上方法的代碼:多線程
1 /** 2 * Post服務請求 3 * 4 * @param requestUrl 請求地址 5 * @param requestbody 請求參數 6 * @return 7 */ 8 public static String sendPost(String requestUrl, String requestbody){ 9 10 try { 11 //創建鏈接 12 URL url = new URL(requestUrl); 13 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 14 15 //設置鏈接屬性 16 connection.setDoOutput(true); //使用URL鏈接進行輸出 17 connection.setDoInput(true); //使用URL鏈接進行輸入 18 connection.setUseCaches(false); //忽略緩存 19 connection.setRequestMethod("POST"); //設置URL請求方法 20 String requestString = requestbody; 21 22 //設置請求屬性 23 byte[] requestStringBytes = requestString.getBytes(); //獲取數據字節數據 24 connection.setRequestProperty("Content-length", "" + requestStringBytes.length); 25 connection.setRequestProperty("Content-Type", "application/octet-stream"); 26 connection.setRequestProperty("Connection", "Keep-Alive");// 維持長鏈接 27 connection.setRequestProperty("Charset", "UTF-8"); 28 29 connection.setConnectTimeout(8000); 30 connection.setReadTimeout(8000); 31 32 //創建輸出流,並寫入數據 33 OutputStream outputStream = connection.getOutputStream(); 34 outputStream.write(requestStringBytes); 35 outputStream.close(); 36 37 //獲取響應狀態 38 int responseCode = connection.getResponseCode(); 39 40 if (HttpURLConnection.HTTP_OK == responseCode) { //鏈接成功 41 //當正確響應時處理數據 42 StringBuffer buffer = new StringBuffer(); 43 String readLine; 44 BufferedReader responseReader; 45 //處理響應流 46 responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 47 while ((readLine = responseReader.readLine()) != null) { 48 buffer.append(readLine).append("\n"); 49 } 50 responseReader.close(); 51 Log.d("HttpPOST", buffer.toString()); 52 return buffer.toString();//成功 53 } 54 }catch (Exception e){ 55 e.printStackTrace(); 56 } 57 return 2;//失敗 58 59 } 60 61 /** 62 * Get服務請求 63 * 64 * @param requestUrl 65 * @return 66 */ 67 public static String sendGet(String requestUrl){ 68 try{ 69 //創建鏈接 70 URL url = new URL(requestUrl); 71 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 72 73 connection.setRequestMethod("GET"); 74 connection.setDoOutput(false); 75 connection.setDoInput(true); 76 77 connection.setConnectTimeout(8000); 78 connection.setReadTimeout(8000); 79 80 connection.connect(); 81 82 //獲取響應狀態 83 int responseCode = connection.getResponseCode(); 84 85 if (HttpURLConnection.HTTP_OK == responseCode) { //鏈接成功 86 //當正確響應時處理數據 87 StringBuffer buffer = new StringBuffer(); 88 String readLine; 89 BufferedReader responseReader; 90 //處理響應流 91 responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 92 while ((readLine = responseReader.readLine()) != null) { 93 buffer.append(readLine).append("\n"); 94 } 95 responseReader.close(); 96 Log.d("HttpGET", buffer.toString()); 97 //JSONObject result = new JSONObject(buffer.toString()); 98 return buffer.toString(); 99 } 100 }catch (Exception e){ 101 e.printStackTrace(); 102 } 103 return null; 104 }
當處理響應流時,返回的可能爲不一樣格式的字符串,這裏方法返回的是String類型的字符串,能夠獲得String類型的返回值後再將其轉換爲JSONObject或JSONArray格式。app
須要注意的是,HTTP請求的方法是不容許在主UI線程中進行的,必定要進行多線程操做。下面是一段簡單的實例:ide
new Thread(new Runnable() { @Override public void run() { String result = HttpUtils.sendPost(BaseHttp.METHOD.ADDLAMP,data); } }).start();
歡迎你們批評指教!url