当前位置:天才代写 > tutorial > 安卓教程 > android http 通信(java原生类库实现)

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

2021-02-27 16:28 星期六 所属: 安卓教程 浏览:44

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);

 

 

    关键字:

天才代写-代写联系方式