Android的HttpUrlConnection类的GET和POST请求

恢弘志士之气,不宜妄自菲薄。这篇文章主要讲述Android的HttpUrlConnection类的GET和POST请求相关的知识,希望能为你提供帮助。

1/** 2* get方法使用 3*/ 4private void httpGet() { 5new Thread() { 6@Override 7public void run() {
//此处的LOGIN是请求地址后面是拼接的参数 8String path = LOGIN + "?phone=12345678900& password=123456"; 9URL url; 10HttpURLConnection connection; 11try { 12url = new URL(path); 13connection = (HttpURLConnection) url.openConnection(); 14connection.setConnectTimeout(4000); //设置链接超时 15connection.setRequestMethod("GET"); //设置请求方法 16 17connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //设置请求体的内容,处处默认也是一样表示请求的是文本内容 18 19int responseCode = connection.getResponseCode(); 20if (responseCode == HttpURLConnection.HTTP_OK) { 21InputStream inputStream = connection.getInputStream(); 22final String s = stremToString(inputStream); 23 24runOnUiThread(new Runnable() { 25@Override 26public void run() { 27Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); 28} 29}); 30inputStream.close(); 31} 32 33} catch (Exception e) { 34e.printStackTrace(); 35} 36} 37}.start(); 38}

1/** 2* post方法 3*/ 4private void httpPost(final Map< String, String> prams) { 5new Thread() { 6@Override 7public void run() { 8if (prams == null) { 9runOnUiThread(new Runnable() { 10@Override 11public void run() { 12Toast.makeText(MainActivity.this, "缺少参数!", Toast.LENGTH_SHORT).show(); 13} 14}); 15return; 16} 17URL url; 18HttpURLConnection connection; 19try { 20//拼接传入的请求参数 21StringBuffer buffer = new StringBuffer(); 22//读取传入的map集合里参数 23for (Map.Entry< String, String> entry : prams.entrySet()) { 24String key = entry.getKey(); 25String value = https://www.songbingjia.com/android/entry.getValue(); 26//拼接参数 例如:phone = 12345678900 & password = 123456 27buffer.append(key +"=" + URLEncoder.encode(value, "utf-8") + "& "); 28} 29//此处是删除末尾拼接的 & 符号 30buffer.deleteCharAt(buffer.length() - 1); 31//REGISTER 是我自己服务器的一个测试请求地址 32url = new URL(REGISTER); 33connection = (HttpURLConnection) url.openConnection(); 34connection.setConnectTimeout(4000); 35 36//此处的输出流表示服务器对客服端的响应输出流 即InPutStream 37//此处的输入流表示 客服端向服务器输入数据即 OutPutStream 38connection.setDoInput(true); //获取服务器的响应输出流 此处默认是true 可以不用设置 39connection.setDoOutput(true); //设置允许向服务其写入数据,获取向服务器的输入流。 40connection.setRequestMethod("POST"); 41//此处设置向服务器请求的内容 请求的是文本内容 默认是可以不用设置的 42connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 43//设置向服务器输入的请求体长度 44connection.setRequestProperty("Content-Length", String.valueOf(buffer.toString().getBytes().length)); 45//向服务器写入请求体 46connection.getOutputStream().write(buffer.toString().getBytes()); 47//获取请求状态吗 HttpURLConnection.HTTP_OK 为请求成功 写200 也可以的 48int responseCode = connection.getResponseCode(); 49if (responseCode == HttpURLConnection.HTTP_OK) { 50InputStream inputStream = connection.getInputStream(); 51final String result = stremToString(inputStream); 52runOnUiThread(new Runnable() { 53@Override 54public void run() { 55Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show(); 56} 57}); 58inputStream.close(); 59} 60 61} catch (Exception e) { 62e.printStackTrace(); 63} 64} 65}.start(); 66}

1/** 2* 把输入流转换成字符串 3* 4* @param inputStream 5* @return 6* @throws IOException 7*/ 8private String stremToString(InputStream inputStream) throws IOException { 9ByteArrayOutputStream bos = new ByteArrayOutputStream(); 10if (inputStream != null) { 11int len; 12byte[] bytes = new byte[1024]; 13while ((len = inputStream.read(bytes)) != -1) { 14bos.write(bytes, 0, len); 15} 16return bos.toString(); 17} else { 18return ""; 19} 20}

【Android的HttpUrlConnection类的GET和POST请求】最后 各位小伙伴们 又不懂或不清楚的可以给我留言 欢迎大家给我提出建议 或是指出问题 我们彼此都需要一个学习的过程


    推荐阅读