在當(dāng)前的互聯(lián)網(wǎng)時(shí)代,移動(dòng)互聯(lián)網(wǎng)已經(jīng)成為最具潛力和前景的市場(chǎng),人們?cè)谶M(jìn)行生活和工作中難免需要使用手機(jī)來(lái)進(jìn)行一些操作,同時(shí),隨著互聯(lián)網(wǎng)技術(shù)的發(fā)展,Web與App之間的數(shù)據(jù)交互顯得越來(lái)越重要。因此,掌握Android與php之間數(shù)據(jù)交互的技術(shù),對(duì)于開(kāi)發(fā)人員來(lái)說(shuō)是十分必要的。
Android是一種使用Java編程語(yǔ)言開(kāi)發(fā)的開(kāi)放源代碼的操作系統(tǒng)。而php是一種被廣泛應(yīng)用于Web環(huán)境中的腳本語(yǔ)言。Android與php之間數(shù)據(jù)交互可以通過(guò)http請(qǐng)求實(shí)現(xiàn),例如使用httpUrlConnection來(lái)發(fā)送http請(qǐng)求,通過(guò)request方法監(jiān)控服務(wù)器數(shù)據(jù)的返回。以下是一種示例代碼的布局:
private String uploadData(String urlString, String params) throws Exception { URL url = new URL(urlString); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0"); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); OutputStream outputStream = httpURLConnection.getOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); bufferedWriter.write(params); bufferedWriter.flush(); InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); StringBuffer response = new StringBuffer(); String line; while ((line = bufferedReader.readLine()) != null) { response.append(line); response.append('\r'); } bufferedReader.close(); return response.toString(); }在進(jìn)行數(shù)據(jù)交互時(shí),需要保證服務(wù)器的接口設(shè)計(jì)合理,同時(shí)保證Android端的請(qǐng)求參數(shù)與服務(wù)器端的返回值保持一致和有意義。例如,在進(jìn)行用戶登錄時(shí),若用戶輸入錯(cuò)誤的賬號(hào)或密碼,則服務(wù)器端需要返回提示信息,而Android客戶端需要根據(jù)服務(wù)器端返回的值來(lái)進(jìn)行后續(xù)的判斷。以下是一種示例代碼的布局:
public static final String LOGIN_URL = "http://192.168.1.2/login.php"; private EditText userName; private EditText userPassword; private class LoginTask extends AsyncTask在以上示例代碼中,onPreExecute方法中獲取了用戶輸入的賬號(hào)和密碼,在doInBackground方法中進(jìn)行數(shù)據(jù)交互,最后在onPostExecute方法中根據(jù)服務(wù)器端返回值進(jìn)行相應(yīng)的操作。 總之,Android與php之間的數(shù)據(jù)交互不僅需要在Android端掌握http請(qǐng)求的基本知識(shí),還需要在服務(wù)器端進(jìn)行接口設(shè)計(jì),同時(shí)注意Android客戶端請(qǐng)求參數(shù)和服務(wù)器端返回值的保持一致和有意義。只要掌握了這些技術(shù),就能大大方便開(kāi)發(fā)人員的應(yīng)用開(kāi)發(fā)工作。{ String username = ""; String password = ""; @Override protected void onPreExecute() { super.onPreExecute(); username = userName.getText().toString(); password = userPassword.getText().toString(); } @Override protected String doInBackground(String... strings) { String response = null; String params = "username=" + username + "&password=" + password; try { response = uploadData(strings[0], params); } catch (Exception e) { e.printStackTrace(); } return response; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if (s == null) { Toast.makeText(getApplicationContext(),"網(wǎng)絡(luò)連接失敗,請(qǐng)檢查您的網(wǎng)絡(luò)",Toast.LENGTH_LONG).show(); return; } if (s == "1") { startActivity(new Intent(getApplicationContext(), MainActivity.class)); } else { Toast.makeText(getApplicationContext(),"用戶名或密碼錯(cuò)誤!",Toast.LENGTH_LONG).show(); } } } public void login(View view) { new LoginTask().execute(LOGIN_URL); }