編輯:關於 Android 程式設計
本文示範如何將表單輸入的資料以 GET 方式傳送到後端 Servlet。重點在於將參數封裝成 Map,拼接成查詢字串後送出請求。
包含標題與時長兩個輸入欄位,以及送出按鈕:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/title" /> <EditText android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/timelength" /> <EditText android:id="@+id/timelength" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" /> </LinearLayout>
package com.example.utils; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class StreamTool { public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); } }
HttpRequest 將 Map 中的鍵值拼接成查詢字串,並建立 HttpURLConnection:
package com.example.web; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; public class HttpRequest { public static void sendGetRequest(String path, Map params) throws Throwable { StringBuilder str = new StringBuilder(); str.append('?'); for (Map.Entry entry : params.entrySet()) { str.append(entry.getKey()).append('=').append(entry.getValue()).append('&'); } str.deleteCharAt(str.length() - 1); URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); } }
點擊按鈕後收集輸入值,放入 Map 並呼叫 HttpRequest:
package com.example.web; import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private static final String TAG = "UploaddataToWen"; private EditText titleText; private EditText timelengthText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image); Button button = (Button) this.findViewById(R.id.button); titleText = (EditText) this.findViewById(R.id.title); timelengthText = (EditText) this.findViewById(R.id.timelength); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String title = titleText.getText().toString(); String timelength = timelengthText.getText().toString(); Map params = new HashMap(); params.put("method", "save"); params.put("title", title); params.put("timelength", timelength); try { HttpRequest.sendGetRequest("https://192.168.1.101:8080/web/admin.do", params); Toast.makeText(MainActivity.this, "save success", Toast.LENGTH_LONG).show(); } catch (Throwable e) { Toast.makeText(MainActivity.this, "save fail", Toast.LENGTH_LONG).show(); Log.e(TAG, e.toString()); } } }); } }
一樣需要網路權限:
<uses-permission android:name="android.permission.INTERNET"/>
實務上,若參數含中文或特殊字元,建議先做 URL 編碼再送出,以避免伺服器端解析異常。
MagicViewPager 上手:六種讓人眼睛一亮的分頁切換動畫
原始碼專案:https://github.com/hongyangAndroid/MagicViewPager 先來看效果,這幾個動畫都挺值得抄的。 Rotate Y(
Android 4.1 移植開發中踩過的四個坑與解法
在 Android 4.1 上手機開發的過程中,累積了幾個比較典型的問題,這裡一併記錄。 問題一:只 push odex 到手機,程式無法運行 解法:修改 dalvik
解決 Android 4.4 Dialog 被狀態欄遮擋的問題
先來看看異常的效果:點擊 tracing_dialog 按鈕後彈出的對話框。 再來看理論上應有的效果: 對比兩張圖可以發現,異常的圖最上方被狀態欄遮擋住了,而這個問題
Android 進階必備:事件分發與攔截機制完整解析
一、Android 控制項架構 二、先給結論 (1)父 View 優先攔截當前事件,攔截不成才輪到子 View 攔截。(2)一旦攔截成功,系統會沿著「子 View