編輯:關於Android編程
兩者的區別如下:
GET上傳的數據一般是很小的並且安全性能不高的數據, 而POST上傳的數據適用於數據量大,數據類型復雜,數據安全性能要求高的地方
GET和POST的使用方法一般如下:
1.采用GET方式向服務器傳遞數據的步驟
1.利用Map集合對數據進行獲取並進行數據處理
if (params!=null&&!params.isEmpty()) {
for (Map.Entry<String, String> entry:params.entrySet()) {
sb.append(entry.getKey()).append("=");
sb.append(URLEncoder.encode(entry.getValue(),encoding));
sb.append("&");
}
sb.deleteCharAt(sb.length()-1);
}
2.新建一個StringBuilder對象
sb=new StringBuilder()
3.新建一個HttpURLConnection的URL對象,打開連接並傳遞服務器的path
connection=(HttpURLConnection) new URL(path).openConnection();
4.設置超時和連接的方式
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
2.采用POST方式向服務器傳遞數據的步驟
1.利用Map集合對數據進行獲取並進行數據處理
if (params!=null&&!params.isEmpty()) {
for (Map.Entry<String, String> entry:params.entrySet()) {
sb.append(entry.getKey()).append("=");
sb.append(URLEncoder.encode(entry.getValue(),encoding));
sb.append("&");
}
sb.deleteCharAt(sb.length()-1);
}
2.新建一個StringBuilder對象,得到POST傳給服務器的數據
sb=new StringBuilder()
byte[] data=sb.toString().getBytes();
3.新建一個HttpURLConnection的URL對象,打開連接並傳遞服務器的path
connection=(HttpURLConnection) new URL(path).openConnection();
4.設置超時和允許對外連接數據
connection.setDoOutput(true);
5.設置連接的setRequestProperty屬性
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", data.length+"");
6.得到連接輸出流
outputStream =connection.getOutputStream();
7.把得到的數據寫入輸出流中並刷新
outputStream.write(data);
outputStream.flush();
3.具體實現的過程如下:
1.使用GET方法上傳數據
服務器中doGet方法中的代碼如下:
[java] view plaincopyprint?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name =request.getParameter("name");
String age=request.getParameter("age");
System.out.println("--------name:"+name);
System.out.println("--------age:"+age);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name =request.getParameter("name");
String age=request.getParameter("age");
System.out.println("--------name:"+name);
System.out.println("--------age:"+age);
}
在客戶端實現的代碼如下:
[java] view plaincopyprint?public class UserSerivce {
public static boolean save(String getname, String getage) throws Exception {
String path = "http://10.254.1.62/WebForGETMethod/ServletForGetMethod";
Map<String, String> params = new HashMap<String, String>();
params.put("name", getname);
params.put("age", getage);
return sendGETRequest(path, params, "UTF-8");
}
private static boolean sendGETRequest(String path,
Map<String, String> params, String encoding) throws Exception {
StringBuilder sb = new StringBuilder(path);
if (params != null && !params.isEmpty()) {
sb.append("?");
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(entry.getKey()).append("=");
sb.append(URLEncoder.encode(entry.getValue(), encoding));
sb.append("&");
}
sb.deleteCharAt(sb.length() - 1);
}
HttpURLConnection connection = (HttpURLConnection) new URL(
sb.toString()).openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
if (connection.getResponseCode() == 200) {
return true;
}
return false;
}
}
public class UserSerivce {
public static boolean save(String getname, String getage) throws Exception {
String path = "http://10.254.1.62/WebForGETMethod/ServletForGetMethod";
Map<String, String> params = new HashMap<String, String>();
params.put("name", getname);
params.put("age", getage);
return sendGETRequest(path, params, "UTF-8");
}
private static boolean sendGETRequest(String path,
Map<String, String> params, String encoding) throws Exception {
StringBuilder sb = new StringBuilder(path);
if (params != null && !params.isEmpty()) {
sb.append("?");
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(entry.getKey()).append("=");
sb.append(URLEncoder.encode(entry.getValue(), encoding));
sb.append("&");
}
sb.deleteCharAt(sb.length() - 1);
}
HttpURLConnection connection = (HttpURLConnection) new URL(
sb.toString()).openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
if (connection.getResponseCode() == 200) {
return true;
}
return false;
}
}
然後呢,就是實現在android客戶端的界面
[java] view plaincopyprint?public class GetDataToWebActivity extends Activity {
private EditText name,age;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getdate);
name=(EditText) findViewById(R.id.name);
age=(EditText) findViewById(R.id.age);
}
public void save(View v) {
String getname=name.getText().toString();
String getage=age.getText().toString();
boolean result=false;
try {
result=UserSerivce.save(getname,getage);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (result) {
Toast.makeText(this, "成功", 1).show();
}else {
Toast.makeText(this, "失敗", 1).show();
}
}
}
public class GetDataToWebActivity extends Activity {
private EditText name,age;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getdate);
name=(EditText) findViewById(R.id.name);
age=(EditText) findViewById(R.id.age);
}
public void save(View v) {
String getname=name.getText().toString();
String getage=age.getText().toString();
boolean result=false;
try {
result=UserSerivce.save(getname,getage);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (result) {
Toast.makeText(this, "成功", 1).show();
}else {
Toast.makeText(this, "失敗", 1).show();
}
}
}
Android應用開發-小巫CSDN博客客戶端Jsoup篇 距上一篇博客已經過去了兩個星期,小巫也覺得非常抱歉,因為在忙著做另外一個項目,幾乎抽不出空來,這不小巫會把剩下
效果圖思路:就是先設置Gridlayout的行列數,然後往裡面放置一定數目的自定義日歷按鈕控件,最後實現日歷邏輯就可以了。步驟:第一步:自定義日歷控件(初步)第二步:實現
前言學習了以上的文章後,接下來我們來講講自定義View,自定義View一直被認為是高手掌握的技能,因為情況太多,想實現的效果又變化多端,但它也要遵循一定的規則,我們要講的
用了微信sdk各種痛苦,感覺比qq sdk調用麻煩多了,回調過於麻煩,還必須要在指定包名下的actvity進行回調,所以我在這裡寫一篇博客,有這個需求的朋友可以借鑒一下,