使用HttpURLConnection采用Post方式请求数据

  • Post author:
  • Post category:其他


1.      服务端

doPost(){

doGet(request,response);

}

2.      Post方式不在URL后面加数据,而是用流的方式传递;GET在URL后传输数据

3.      是否传递—请求头:setRequestProperty();

MainActivity中:



  1. public




    void


    doPost(View v) {



  2. final


    String userName = etUserName.getText().toString();



  3. final


    String password = etPassword.getText().toString();




  4. new


    Thread(


    new


    Runnable() {



  5. @Override





  6. public




    void


    run() {



  7. final


    String state = NetUtils.loginOfPost(userName, password);



  8. // 执行任务在主线程中




  9. runOnUiThread(

    new


    Runnable() {



  10. @Override





  11. public




    void


    run() {



  12. // 就是在主线程中操作




  13. Toast.makeText(MainActivity.

    this


    , state,


    0


    ).show();


  14. }

  15. });

  16. }

  17. }).start();

  18. }


loginOfPost方法:



  1. /**



  2. * 使用post的方式登录



  3. * @param userName



  4. * @param password



  5. * @return



  6. */





  7. public




    static


    String loginOfPost(String userName, String password) {


  8. HttpURLConnection conn =

    null


    ;



  9. try


    {


  10. URL url =

    new


    URL(


    “http://10.0.2.2:8080/ServerItheima28/servlet/LoginServlet”


    );



  11. conn = (HttpURLConnection) url.openConnection();


  12. conn.setRequestMethod(

    “POST”


    );


  13. conn.setConnectTimeout(

    10000


    );


    // 连接的超时时间




  14. conn.setReadTimeout(

    5000


    );


    // 读数据的超时时间




  15. conn.setDoOutput(

    true


    );


    // 必须设置此方法, 允许输出****** 默认情况下, 系统不允许向服务器输出内容




  16. /           conn.setRequestProperty(

    “Content-Length”


    ,


    234


    );


    // 设置请求头消息, 可以设置多个






  17. // post请求的参数




  18. String data =

    “username=”


    + userName +


    “&password=”


    + password;




  19. // 获得一个输出流, 用于向服务器写数据, 默认情况下, 系统不允许向服务器输出内容




  20. OutputStream out = conn.getOutputStream();

  21. out.write(data.getBytes());

  22. out.flush();

  23. out.close();



  24. int


    responseCode = conn.getResponseCode();



  25. if


    (responseCode ==


    200


    ) {


  26. InputStream is = conn.getInputStream();

  27. String state = getStringFromInputStream(is);


  28. return


    state;


  29. }

    else


    {


  30. Log.i(TAG,

    “访问失败: ”


    + responseCode);


  31. }

  32. }

    catch


    (Exception e) {


  33. e.printStackTrace();

  34. }

    finally


    {



  35. if


    (conn !=


    null


    ) {


  36. conn.disconnect();

  37. }

  38. }


  39. return




    null


    ;


  40. }




根据流返回一个字符串信息;




  1. /**



  2. * 根据流返回一个字符串信息



  3. * @param is



  4. * @return



  5. * @throws IOException



  6. */





  7. private




    static


    String getStringFromInputStream(InputStream is)


    throws


    IOException {


  8. ByteArrayOutputStream baos =

    new


    ByteArrayOutputStream();



  9. byte


    [] buffer =


    new




    byte


    [


    1024


    ];



  10. int


    len = –


    1


    ;




  11. while


    ((len = is.read(buffer)) != –


    1


    ) {


  12. baos.write(buffer,

    0


    , len);


  13. }

  14. is.close();


  15. String html = baos.toString();

    // 把流中的数据转换成字符串, 采用的编码是: utf-8






  16. //      String html = new String(baos.toByteArray(), “GBK”);





  17. baos.close();


  18. return


    html;


  19. }