protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String str = "你好-kobe"; response.getOutputStream().write(str.getBytes("utf-8"));//如果不指定utf-8编码,以默认gbk编码传值,需要在android端处理 }
private void send2Server() { String path = "http://192.168.8.19:8080/LoginServlet"; AsyncHttpClient client = new AsyncHttpClient(); client.get(path, new AsyncHttpResponseHandler() { @Override public void onSuccess(int i, Header[] headers, byte[] bytes) { try { //接收Servlet传回的值 String str = new String(bytes,"utf-8");//默认是utf-8,可以不写 //String str = new String(bytes,"gbk");//如果服务器不指定utf-8,需要在客户端用gbk解 Log.i("log", str ); Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printStackTrace(); } } @Override public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { Log.i("log", "onFailure"); } }); }
时间: 2024-10-11 07:34:15