2011年11月29日 星期二

android 程式使用post或get傳送至server

1.manifest.xml增加internet permission
  • 使用get方法:
String url="http://www.google.com/guestbook.jsp"?name=ox&id=100;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);

  • 使用post方法
String url="http://www.google.com/guestbook.jsp";
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
List pair=new ArrayList();
pair.add(new BasicNameValuePair("name","ox"));
pair.add(new BasicNameValuePair("id","100"));
try
{
request.setEntity(new UrlEncodedFormEntity(pair));
} catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); }