標籤:android post 鄭傳餘
1.get是從伺服器上擷取資料,post是向伺服器傳送資料。
2.get是把參數資料隊列加到提交表單的 ACTION屬性所指的URL中,值和表單內各個欄位一一對應,在URL中可以看到。post是通過HTTPpost機制,將表單內各個欄位與其內容放置在HTML HEADER內一起傳送到ACTION屬性所指的URL地址。使用者看不到這個過程。
3.對於get方式,伺服器端用 Request.QueryString擷取變數的值,對於post方式,伺服器端用Request.Form擷取提交的資料。
4.get 傳送的資料量較小,不能大於2KB。post傳送的資料量較大,一般被預設為不受限制。但理論上,IIS4中最大量為80KB,IIS5中為100KB。
5.get安全性非常低,post安全性較高。
優酷:http://v.youku.com/v_show/id_XODU1MzExMjUy.html
愛奇藝:http://www.iqiyi.com/w_19rsechp2t.html#vfrm=8-7-0-1
以下是通過post發送資料,完成在用戶端登陸模組,當然這得搭建伺服器,如果有對伺服器不熟悉的同學,可以發送郵件到[email protected],很樂意協助大家,以下是MainActivity.java,布局檔案不再給出;
public class MainActivity extends Activity {
private EditText ed_name;
private EditText ed_password;
private Button btn_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_name = (EditText)findViewById(R.id.username);
ed_password = (EditText)findViewById(R.id.upassword);
btn_login = (Button)findViewById(R.id.btn_login);
btn_login.setOnClickListener(new MyButtonListener());
}
class MyButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
String name = ed_name.getText().toString();
String password = ed_password.getText().toString();
PostThread pt = new PostThread(name, password);
pt.start();
}
}
/*登陸成功放回伺服器字元
* 登陸失敗則放回不了???
*
*/
//post 方法 發送資料到伺服器
class PostThread extends Thread{
String name;
String password;
public PostThread(String name, String password) {
super();
this.name = name;
this.password = password;
}
@Override
public void run() {
super.run();
HttpClient httpClient = new DefaultHttpClient();
String url = "http://192.168.191.1:8080/2.5/LoginServlet";
HttpPost httpPost = new HttpPost(url);
//NameValuePair對象,代表了一個需要發往伺服器的索引值對
NameValuePair pair1 = new BasicNameValuePair("username", name);
NameValuePair pair2 = new BasicNameValuePair("upassword", password);
//使用集合類將其封裝起來
ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(pair1);
pairs.add(pair2);
//請求體代表了了請求對象
try {
HttpEntity requestEntity = new UrlEncodedFormEntity(pairs);
//將請求體放置在請求對象中
httpPost.setEntity(requestEntity);
HttpResponse response = httpClient.execute(httpPost);
if(response.getStatusLine().getStatusCode() == 200){
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
String reslut = reader.readLine();
System.out.println(reslut);
Log.d("HttpPost","result"+reslut);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
本文出自 “7851921” 部落格,請務必保留此出處http://7861921.blog.51cto.com/7851921/1595032
android 通過post發送資料 完成用戶端登陸模組