标签:timeout tin length map tac 请求 output thread pen
1 /** 2 * get方法使用 3 */ 4 private void httpGet() { 5 new Thread() { 6 @Override 7 public void run() {
//此处的LOGIN是请求地址后面是拼接的参数 8 String path = LOGIN + "?phone=12345678900&password=123456"; 9 URL url; 10 HttpURLConnection connection; 11 try { 12 url = new URL(path); 13 connection = (HttpURLConnection) url.openConnection(); 14 connection.setConnectTimeout(4000);//设置链接超时 15 connection.setRequestMethod("GET");//设置请求方法 16 17 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//设置请求体的内容,处处默认也是一样表示请求的是文本内容 18 19 int responseCode = connection.getResponseCode(); 20 if (responseCode == HttpURLConnection.HTTP_OK) { 21 InputStream inputStream = connection.getInputStream(); 22 final String s = stremToString(inputStream); 23 24 runOnUiThread(new Runnable() { 25 @Override 26 public void run() { 27 Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); 28 } 29 }); 30 inputStream.close(); 31 } 32 33 } catch (Exception e) { 34 e.printStackTrace(); 35 } 36 } 37 }.start(); 38 }
1 /** 2 * post方法 3 */ 4 private void httpPost(final Map<String, String> prams) { 5 new Thread() { 6 @Override 7 public void run() { 8 if (prams == null) { 9 runOnUiThread(new Runnable() { 10 @Override 11 public void run() { 12 Toast.makeText(MainActivity.this, "缺少参数!", Toast.LENGTH_SHORT).show(); 13 } 14 }); 15 return; 16 } 17 URL url; 18 HttpURLConnection connection; 19 try { 20 //拼接传入的请求参数 21 StringBuffer buffer = new StringBuffer(); 22 //读取传入的map集合里参数 23 for (Map.Entry<String, String> entry : prams.entrySet()) { 24 String key = entry.getKey(); 25 String value = entry.getValue(); 26 //拼接参数 例如:phone = 12345678900 & password = 123456 27 buffer.append(key + "=" + URLEncoder.encode(value, "utf-8") + "&"); 28 } 29 //此处是删除末尾拼接的 & 符号 30 buffer.deleteCharAt(buffer.length() - 1); 31 //REGISTER 是我自己服务器的一个测试请求地址 32 url = new URL(REGISTER); 33 connection = (HttpURLConnection) url.openConnection(); 34 connection.setConnectTimeout(4000); 35 36 //此处的输出流表示 服务器对客服端的响应输出流 即InPutStream 37 //此处的输入流表示 客服端向服务器输入数据即 OutPutStream 38 connection.setDoInput(true);//获取服务器的响应输出流 此处默认是true 可以不用设置 39 connection.setDoOutput(true);//设置允许向服务其写入数据,获取向服务器的输入流。 40 connection.setRequestMethod("POST"); 41 //此处设置向服务器请求的内容 请求的是文本内容 默认是可以不用设置的 42 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 43 //设置向服务器输入的请求体长度 44 connection.setRequestProperty("Content-Length", String.valueOf(buffer.toString().getBytes().length)); 45 //向服务器写入请求体 46 connection.getOutputStream().write(buffer.toString().getBytes()); 47 //获取请求状态吗 HttpURLConnection.HTTP_OK 为请求成功 写200 也可以的 48 int responseCode = connection.getResponseCode(); 49 if (responseCode == HttpURLConnection.HTTP_OK) { 50 InputStream inputStream = connection.getInputStream(); 51 final String result = stremToString(inputStream); 52 runOnUiThread(new Runnable() { 53 @Override 54 public void run() { 55 Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show(); 56 } 57 }); 58 inputStream.close(); 59 } 60 61 } catch (Exception e) { 62 e.printStackTrace(); 63 } 64 } 65 }.start(); 66 }
1 /** 2 * 把输入流转换成字符串 3 * 4 * @param inputStream 5 * @return 6 * @throws IOException 7 */ 8 private String stremToString(InputStream inputStream) throws IOException { 9 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 10 if (inputStream != null) { 11 int len; 12 byte[] bytes = new byte[1024]; 13 while ((len = inputStream.read(bytes)) != -1) { 14 bos.write(bytes, 0, len); 15 } 16 return bos.toString(); 17 } else { 18 return ""; 19 } 20 }
最后 各位小伙伴们 又不懂或不清楚的可以给我留言 欢迎大家给我提出建议 或是指出问题 我们彼此都需要一个学习的过程
Android的HttpUrlConnection类的GET和POST请求
标签:timeout tin length map tac 请求 output thread pen
原文地址:http://www.cnblogs.com/hejiaoshou/p/7460714.html