博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
复习URLHttpConnection方式GET,POST方式链接网络解析uri
阅读量:5104 次
发布时间:2019-06-13

本文共 6213 字,大约阅读时间需要 20 分钟。

xml:

1 
6 7
14
22 23
30 31
38 39

 

NetUtils:

1 package com.xh.tx.netUtils;  2   3 import java.io.ByteArrayOutputStream;  4 import java.io.IOException;  5 import java.io.InputStream;  6 import java.io.OutputStream;  7 import java.net.HttpURLConnection;  8 import java.net.ProtocolException;  9 import java.net.URL; 10 import java.net.URLEncoder; 11  12 public class NetUtils  13 { 14     public static String getSubmit(String username,String password,String uri) 15     { 16         uri = uri +"?username=" + username + "&password=" + password; 17          18         HttpURLConnection conn =  getHttpURLConnection(uri); 19         // http://localhost:8080/TestServlet?username=zs&password=123 20          21         try { 22             conn.setConnectTimeout(30000); 23             conn.setReadTimeout(30000); 24             conn.setRequestMethod("GET"); 25              26             conn.connect(); //连接 连接的时候是否要传递参数过去 27              28             //先判断一下状态是否为200,如果为200则将in流转换为字符串 29             if(conn.getResponseCode() == 200) 30             { 31                 String content = getStringFromInputStream(conn.getInputStream()); 32                  33                 return content; 34             } 35              36         } catch (ProtocolException e) { 37             e.printStackTrace(); 38         } catch (IOException e) { 39             e.printStackTrace(); 40         } 41          42         return null; 43     } 44      45     private static String getStringFromInputStream(InputStream inputStream) throws IOException 46     { 47         byte[] buffer = new byte[1024]; 48         ByteArrayOutputStream bytearray = new ByteArrayOutputStream(); 49         int len = 0; 50          51         while((len = inputStream.read(buffer, 0, 1024)) != -1) 52         { 53             bytearray.write(buffer); 54         } 55          56     //    String content = new String(bytearray.toByteArray(),"GBK"); 57          58         return bytearray.toString(); 59     } 60  61     public static String postSubmit(String username,String password, String uri) 62     { 63         HttpURLConnection conn =  getHttpURLConnection(uri); 64          65         try { 66             conn.setConnectTimeout(30000); 67             conn.setReadTimeout(30000); 68             conn.setRequestMethod("POST"); 69             //如果你要兼容2.3版本,那么你必须添加一下这句话 70             conn.setDoInput(true); 71              72             //参数传递 73             OutputStream out = conn.getOutputStream(); 74              75             conn.connect(); 76             out.write(("username="+username + "&password=" + password).getBytes()); 77              78             if(conn.getResponseCode() == 200) 79             { 80                 String content = getStringFromInputStream(conn.getInputStream()); 81                 return content; 82             } 83         } catch (ProtocolException e) { 84             e.printStackTrace(); 85         } catch (IOException e) { 86             e.printStackTrace(); 87         } 88          89         return null; 90     } 91      92     public static HttpURLConnection getHttpURLConnection(String uri) 93     { 94         try { 95             URL url = new URL(uri); 96             HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 97              98             return conn; 99         } catch (IOException e) {100             e.printStackTrace();101         }102         return null;103         104     }105 }

MainActivity:

1 package com.xh.tx.postget; 2  3 import com.xh.tx.netUtils.NetUtils; 4  5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 import android.view.View;10 import android.view.View.OnClickListener;11 import android.widget.EditText;12 import android.widget.Toast;13 14 public class MainActivity extends Activity implements OnClickListener {15 16     EditText et_username;17     EditText et_password;18     19     @Override20     protected void onCreate(Bundle savedInstanceState) {21         super.onCreate(savedInstanceState);22         setContentView(R.layout.activity_main);23         24         et_username = (EditText) findViewById(R.id.et_username);25         et_password = (EditText) findViewById(R.id.et_password);26         27         findViewById(R.id.bt_get).setOnClickListener(this);28         findViewById(R.id.bt_post).setOnClickListener(this);29         30     }31 32     @Override33     public void onClick(View v) 34     {35         final String username = et_username.getText().toString();36         final String password = et_password.getText().toString();37         38         switch (v.getId()) {39         case R.id.bt_get:40             new Thread(new Runnable()41             {42                 @Override43                 public void run()44                 {45                     final String status = NetUtils.getSubmit(username, password,"http://10.0.2.2:8080/baidu/LoginServelt");46                     47                     runOnUiThread(new Runnable() {48                         @Override49                         public void run() 50                         {51                             Toast.makeText(MainActivity.this, "返回的状态为:" + status, 0).show();52                         }53                     });54                 }55             }).start();56             break;57         case R.id.bt_post:58             new Thread(new Runnable()59             {60                 @Override61                 public void run()62                 {63                     final String status = NetUtils.postSubmit(username, password,"http://10.0.2.2:8080/baidu/LoginServelt");64                     65                     runOnUiThread(new Runnable() {66                         @Override67                         public void run() 68                         {69                             Toast.makeText(MainActivity.this, "返回的状态为:" + status, 0).show();70                         }71                     });72                 }73             }).start();74             break;75 76         default:77             break;78         }79     }80 }

 

转载于:https://www.cnblogs.com/zzw1994/p/4918043.html

你可能感兴趣的文章
GDOI DAY1游记
查看>>
收集WebDriver的执行命令和参数信息
查看>>
数据结构与算法(三)-线性表之静态链表
查看>>
mac下的mysql报错:ERROR 1045(28000)和ERROR 2002 (HY000)的解决办法
查看>>
快速幂
查看>>
改善C#公共程序类库质量的10种方法
查看>>
AIO 开始不定时的抛异常: java.io.IOException: 指定的网络名不再可用
查看>>
MyBaits动态sql语句
查看>>
HDU4405(期望DP)
查看>>
拉格朗日乘子法 那些年学过的高数
查看>>
vs code 的便捷使用
查看>>
Spring MVC @ResponseBody返回中文字符串乱码问题
查看>>
用户空间与内核空间,进程上下文与中断上下文[总结]
查看>>
JS 中的跨域请求
查看>>
JAVA开发环境搭建
查看>>
mysql基础语句
查看>>
Oracle中的rownum不能使用大于>的问题
查看>>
[Data Structure & Algorithm] 有向无环图的拓扑排序及关键路径
查看>>
cassandra vs mongo (1)存储引擎
查看>>
Visual Studio基于CMake配置opencv1.0.0、opencv2.2
查看>>