Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android編程入門 >> android http 通信(java原生類庫實現)

android http 通信(java原生類庫實現)

編輯:Android編程入門

get方式

private void doGet(){
  URL httpurl = new URl(url);   
  HttpURLConnection  conn = (HttpURLConnection)httpUrl.openConnection();
  conn.setRequestMethod("GET");
  conn.setReadTimeout(5000);
  BufferedReader reader = new BuffedReader(new InputStreamReader(conn.getInputStream));
  String str;
  StringBuffer sb = new StringBuffer();
  while((str = reader.readLine()) != null){
     sb.append(str);  
  }
}

post方式

private void doPost(){
  URL httpUrl = new URL(url);
  HttpURLconnection conn = (HttpURLConnection)  httpUrl.openConnection();
  conn.setRequestMethod("POST");
  conn.setReadTimeout(5000);
  OutputStream out = conn.getOutputStream();
  String content = "name"+name+"age"+age;
  out.write(content.getBytes());   BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));   StringBuffer sb = new StrintgBuffer();   String str;   while((str = reader.readLine())!=null){     sb.append(str);   } }

服務器端解決亂碼問題

String name = request.getParameter("name");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
name = new String(name.getBytes("iso-8859-1"),"utf-8");

android解決亂碼問題

get方式:URLEncoder.encode(name,"utf-8");
post方式:不會產生亂碼,因為android系統默認使用utf-8編碼

獲取android系統默認配置信息

Properties property = System.getProperties();
property.list(System.out);

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved