編輯:關於 Android 程式設計
在本次項目中,我用 apache-tomcat 把自己的計算機變成了一台小服務器,然後對裡面的 jsp 類型文件進行讀寫。
首先,如何搭建服務器呢?
1. 下載一個 apache-tomcat,這裡給大家提供一個 apache-tomcat-6.0.37 的下載地址:點擊打開鏈接,提取碼:utb8。
2. 下載好之後解壓,進入文件夾,運行 apache-tomcat-6.0.37/bin/startup.dat,如下圖所示。



3. 打開該文件後耐心等待,等進程完成後,打開瀏覽器,在地址欄鍵入 localhost:8080 並跳轉。如果能正常訪問到那個有貓的界面,那麼恭喜,服務器已建立成功。
服務器建好後,接下來就該讀取其中的數據了。
這裡的布局我設了一個 EditView 用於輸入 IP 地址,兩個按鈕分別代表不同的獲取信息方式,而文本框則用於顯示讀取到的信息。
接下來是 Activity,裡面有詳細的註釋,我就不在此贅述了。
public class HttpActivity extends AppCompatActivity { private EditText et ; private Button search1; private Button search2; private TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_http); et = (EditText) findViewById(R.id.et); search1 = (Button) findViewById(R.id.search1); search2 = (Button) findViewById(R.id.search2); show = (TextView) findViewById(R.id.show); search1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String url = "https://"+et.getText()+":8080/HttpTest/index.jsp" + "?option=getUser&uName=jerehedu"; new MyGetJob().execute(url); } }); search2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String[] arg = new String[2]; arg[0] = "https://"+et.getText()+":8080/HttpTest/index.jsp"; arg[1] = "option=getUser&uName=jerehedu"; new MyPostJob().execute(arg); } }); } public class MyPostJob extends AsyncTask{ @Override protected String doInBackground(String... strings) { HttpURLConnection httpURLConnection = null; InputStream is = null; StringBuilder sbd = new StringBuilder(); try { URL url = new URL(strings[0]); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(5*1000); httpURLConnection.setReadTimeout(5*1000); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); //是否保存用戶緩存 httpURLConnection.setUseCaches(false); //在這裡設置編碼方式 httpURLConnection.setRequestProperty("Charset","UTF-8"); httpURLConnection.setRequestProperty("Content-type","application/x-www-form-urlencoded"); //params應該是這樣的樣式: option=getUser&Name=jerehedu (沒了問號) String params = strings[1]; OutputStream os = httpURLConnection.getOutputStream(); os.write(params.getBytes()); os.flush(); os.close(); if(httpURLConnection.getResponseCode()==200){ is=httpURLConnection.getInputStream(); int next = 0; byte[] bt = new byte[1024]; while ((next=is.read(bt))>0){ sbd.append(new String(bt,0,next)); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(httpURLConnection!=null){ httpURLConnection.disconnect(); } } return sbd.toString(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); show.setText("POST請求結果:"+s); } } /* AsyncTask異步任務類 異步任務類的第一個參數會傳到doInBackground方法中 第三個參數: 此為指定doInBackground方法的返回值。 onPostExecute方法中的String s是來自doInBackground方法的返回值。 UI線程裡聲明的任何變量,在子線程中禁止操作。 異步任務類裡只有5個方法。 */ public class MyGetJob extends AsyncTask{ //onPreExecute在主線程中執行命令,它會在doInBackground前執行。 //通常會在這裡做進度條的初始化,例如:下載進度 @Override protected void onPreExecute() { super.onPreExecute(); } //必須實現的方法,可以理解成,在子線程中執行命令 @Override //這裡的...代表數組 protected String doInBackground(String... strings) { HttpURLConnection httpURLConnection = null; InputStream is = null; StringBuilder sbd = new StringBuilder(); try { URL url = new URL(strings[0]); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(5*1000); httpURLConnection.setReadTimeout(5*1000); /* http 響應碼 200:成功 404:未找到 500:發生錯誤 */ if(httpURLConnection.getResponseCode()==200){ is=httpURLConnection.getInputStream(); int next = 0; byte[] bt = new byte[1024]; while ((next=is.read(bt))>0){ sbd.append(new String(bt,0,next)); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(httpURLConnection!=null){ httpURLConnection.disconnect(); } } return sbd.toString(); } //在UI線程(主線程)中執行命令 @Override protected void onPostExecute(String s) { super.onPostExecute("GET請求結果:"+s); show.setText(s); } } }
在運行時,需要我們輸入 IP 地址,即服務器的 IP 地址,怎麼獲得呢?
首先打開「運行」——Windows+R,然後輸入 CMD 並確認,之後再輸入 ipconfig 回車,就可以看到 IP 地址了,如下圖所示:



輸入 IP 地址,點擊按鈕,效果圖如下:


Android自定義View實戰:打造帶數字的進度條NumberProgressBar
第一步、效果展示 圖1、藍色的進度條 圖2、紅色的進度條 圖3、多條顏色不同的進度條 圖4、多條顏色不同的進度條 版權聲明:本文為【歐陽鵬】原創文章,歡迎
[Android] 你真的了解 Activity 嗎?
Activity 到底是什麼 Android 四大組件——Activity、Service、ContentProvider、BroadcastReceiver——其中 A
Android 將 Activity 背景設為透明的方法
讓 Activity 背景透明有時我們會希望某個 Activity 沒有實色背景,能看見背後的畫面或桌面。Android 提供 Theme.Translucent 系列主
從零實現到開源:Android 自定義圓形漸變色進度條全記錄
信自己,也是一種信仰。 寫在前面的話:3 月初我在自定義控件概述裡挖下的幾個坑,前段時間已基本填完,幾種實現方式也各自寫了 demo 說明。今天來聊聊如何把一個自封裝的