編輯:關於Android編程
***1.服務器端
最簡單的服務器端接收代碼,直接把穿過倆的數據打印*
package cn.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyServlet
*/
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//堅持調用post方法
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//獲得數據
String name = request.getParameter("name");
String age = request.getParameter("age");
//打印數據
System.out.println(name);
System.out.println(age);
}
}
sevlet
MyServlet
cn.servlet.MyServlet
MyServlet
/ss
2.客戶端
首先我們自定義一個線程
public class Thread1 extends Thread
//數據來自布局的文本編輯框
String name ;
String age;
String url;
public Thread1(String name, String age,String url){
this.name=name;
this.age=age;
this.url=url;
}
1)get方法的實現
public void doGet(){
try {
//get方法的特別注意的地方
url=url+"?name="+URLEncoder.encode(name, "utf-8")+"&age="+age;
//轉換為url對象
URL httpUrl = new URL(url);
//獲取連接對象
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
//設置傳輸方法
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
//讀取輸入流
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String str;
StringBuffer sb = new StringBuffer();
//分別放入sb
while((str=br.readLine())!=null){
sb.append(str);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2)post方法的實現
public void doPost(){
try {
URL httpUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
conn.setRequestMethod("POST");
conn.setReadTimeout(5000);
//注意這裡是conn.getoutputstream,而不是new出來的,這樣才能關聯起來
OutputStream out =conn.getOutputStream();
String content = "name="+name+"&age="+age;
out.write(content.getBytes());
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String str;
StringBuffer sb = new StringBuffer();
while((str=br.readLine())!=null){
sb.append(str);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在run方法裡面執行的是哪個方法就使用相應的方式
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
doGet();
// doPost();
}
3)客戶端輸入數據的布局簡單布置了一下就是兩個編輯文本框和一個按鈕
private EditText etName,etAge;
private Button btnReg;
4)按鈕的點擊事件
btnReg.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
//把ip地址改為自己目前的
String url = "http://192.168.191.1:8080/serve/ss";
new Thread1(etName.getText().toString(), etAge.getText().toString(), url).start();
}
});
}
1.onKeyDown 方法 onKeyDown 方法是KeyEvent.Callback 接口中的一個抽象方法,重寫onKeyDown 方法可以監聽到按鍵被按下的事件,
1. Why,為什麼要加載縮略圖? 有的時候不需要展示原圖,只需展示圖片的縮略圖,可以節省內存。比如:網易新聞中的圖片浏覽,左邊展示的小獅子圖片就是一個縮略
一 前言??把android開發藝術探索第三章閱讀了,對事件分發機制有一個大概的了解,關於事件分發的文章也很多,自己也看了一些相關的文章,決定自己分析一遍記錄下來,加深映
onTach介紹ontach是Android系統中整個事件機制的基礎。Android中的其他事件,如onClick、onLongClick等都是以onTach為基礎的。o