android http 通信(java原生类库实现)

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);
上一篇:JavaScript三种判断语句和三元运算符


下一篇:CentOS 6.0 设置IP地址、网关、DNS