[android] 13_GET方式提交数据-HttpUrlConnection

Android 4.0

GET方式提交数据


核心代码:

1、Android端

// 开启一个新的线程,发送请求到服务器
new   Thread ()   {
     @Override
     public   void   run ()   {
         super . run ();
         try   {
             String   result   =   LoginService . loginByGet ( username ,   password );
             System . out . println ( "登录状态:" + result );
         }   catch   ( IOException   e )   {
             e . printStackTrace ();
         }
     }
}. start ();

/**
* 1、GET方式进行登录
*
* @throws IOException
*/

public static String loginByGet(String username, String password)
        throws IOException {
 
    username = URLEncoder.encode(username, "utf-8");
    String path = "http://192.168.221.221:8080/web/LoginServlet?username="+username+"&password="+password;
 
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5000);
    conn.setReadTimeout(5000);
 
    int code = conn.getResponseCode();
    if (code == 200) {
        InputStream in = conn.getInputStream();
        String text = StrStreamUtils.streamToText(in);
        return text;
    } else {
        return null;
    }
}
 
public class StrStreamUtils {
    /**
     * 将输入流转成文本
     * @param in
     * @return
     */
    public static String streamToText(InputStream in) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len = 0;
        byte[] buf = new byte[1024];
        try {
            while ((len = in.read(buf)) != -1) {
                baos.write(buf, 0, len);
            }
            byte[] byteArray = baos.toByteArray();
            return new String(byteArray);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
        
2、web端
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        username = new String(username.getBytes("iso8859-1"), "utf-8");
 
        System.out.println("用户名:" + URLDecoder.decode(username,"utf-8"));
        System.err.println("密码:" + password);
 
        // 模拟查询数据库
        if ("hacket".equals(username) && "123456".equals(password)) {
            // 返回登陆成功
            response.getOutputStream().write("登录成功".getBytes("utf-8"));
        } else {
            // 返回登陆失败
            response.getOutputStream().write("登录失败".getBytes("utf-8"));
        }
    }

 

android:

乱码1:response中的中文,出现的乱码,由于服务器和Android端的编码不一致

服务器:response.getOutputStream().write("登录成功".getBytes("utf-8"));

Android客户端:new String(byteArray);

乱码2:request提交中有中文,出现的乱码

1、服务端:会自动将中文数据url编码
GET /web/LoginServlet?username=%E5%B0%8F%E8%83%9C&password=123456 HTTP/1.1
 
username = new String(username.getBytes("iso8859-1"), "utf-8"); //但如果直接在浏览器中输入就会有乱码
System.out.println("用户名:" + URLDecoder.decode(username,"utf-8"));

2、android端提交:需要手动写

username = URLEncoder.encode(username, "utf-8");

String path = "http://192.168.221.221:8080/web/LoginServlet?username="+username+"&password="+password;