1       建立Http连接的步骤:

1.1      获得一个http的连接地址(如:String httpurl = "http://192.168.0.68:8090/Test/index.jsp?par=this-is-get-Method-request!";)

1.2      构造一个URL对象(如:url = new URL(httpurl);)

1.3      使用HttpURLConnection打开一个连接(如:HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();)

1.4      得到读取的内容(流)(如:InputStream inputStream = httpConnection.getInputStream();)

2     TestHttpGetPostActivity:

import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLEncoder;

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;

public class TestHttpGetPostActivity extends Activity{private Button mButton01;private Button mButton02;private Button mButton03;private String data = null;private TextView mTextView;private Intent intent = new Intent();    @Overridepublic void onCreate(Bundle savedInstanceState)    {super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mButton01 = (Button) findViewById(R.id.get);        mButton02 = (Button) findViewById(R.id.post);        mButton03 = (Button) findViewById(R.id.nothing);        mTextView = (TextView) findViewById(R.id.textview);        mButton01.setOnClickListener(listener);        mButton02.setOnClickListener(listener);        mButton03.setOnClickListener(listener);    }

private OnClickListener listener = new OnClickListener()    {        @Overridepublic void onClick(View v)        {switch (v.getId())            {// get请求                case R.id.get:try                    {                        String httpurl = "http://192.168.0.68:8090/Test/index.jsp?par=this-is-get-Method-request!";                        URL url;                        url = new URL(httpurl);if (url != null)                        {// 使用HttpURLConnection打开网络连接                            HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();                            InputStream inputStream = httpConnection.getInputStream();                            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));while (bufferedReader.readLine() != null)                            {                                data += bufferedReader.readLine() + "\n";                            }// 关闭inputStream                            inputStream.close();// 关闭http连接                            httpConnection.disconnect();                            System.out.println("data = " + data);if (data != null)                            {                                intent.putExtra("data", data);                                intent.setClass(TestHttpGetPostActivity.this, ShowHtmlContent.class);                                startActivity(intent);                            }else                            {                                mTextView.setText("data is NULL!");                            }                        }                    }catch (MalformedURLException e)                    {                        e.printStackTrace();                    }catch (IOException e)                    {                        mTextView.setText("url is NULL!");                        e.printStackTrace();                    }break;

// post请求                case R.id.post:try                    {                        String httpurl = "http://192.168.0.68:8090/Test/index.jsp";                        URL url;                        url = new URL(httpurl);if (url != null)                        {// 使用HttpURLConnection打开网络连接                            HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();// 因为是post请求,需要设置成true                            httpConnection.setDoInput(true);                            httpConnection.setDoOutput(true);// 设置post请求方式                            httpConnection.setRequestMethod("POST");// post请求不能使用缓存                            httpConnection.setUseCaches(false);                            httpConnection.setInstanceFollowRedirects(true);

// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded                            httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成// 要注意的是connection.getOutputSream会隐含地进行connect.urlConn.connect();// DataOutputSream流                            DataOutputStream outputStream = new DataOutputStream(httpConnection.getOutputStream());// 要上传的参数                            String content = "par=" + URLEncoder.encode("this is post request!", "gb2312");// 将要上传的内容写入流中                            outputStream.writeBytes(content);// 刷新,关闭                            outputStream.flush();                            outputStream.close();                            InputStream inputStream = httpConnection.getInputStream();                            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));while (bufferedReader.readLine() != null)                            {                                data += bufferedReader.readLine() + "\n";                            }// 关闭inputStream                            inputStream.close();// 关闭http连接                            httpConnection.disconnect();                            System.out.println("data = " + data);if (data != null)                            {                                intent.putExtra("data", data);                                intent.setClass(TestHttpGetPostActivity.this, ShowHtmlContent.class);                                startActivity(intent);                            }else                            {                                mTextView.setText("data is NULL!");                            }                        }                    }catch (MalformedURLException e)                    {                        e.printStackTrace();                    }catch (IOException e)                    {                        mTextView.setText("url is NULL!");                        e.printStackTrace();                    }break;case R.id.nothing:try                    {                        String httpurl = "http://192.168.0.68:8090/Test/index.jsp";                        URL url;                        url = new URL(httpurl);if (url != null)                        {// 使用HttpURLConnection打开网络连接                            HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();                            InputStream inputStream = httpConnection.getInputStream();                            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));while (bufferedReader.readLine() != null)                            {                                data += bufferedReader.readLine() + "\n";                            }// 关闭inputStream                            inputStream.close();// 关闭http连接                            httpConnection.disconnect();                            System.out.println("data = " + data);if (data != null)                            {                                intent.putExtra("data", data);                                intent.setClass(TestHttpGetPostActivity.this, ShowHtmlContent.class);                                startActivity(intent);                            }else                            {                                mTextView.setText("data is NULL!");                            }                        }                    }catch (MalformedURLException e)                    {                        e.printStackTrace();                    }catch (IOException e)                    {                        mTextView.setText("url is NULL!");                        e.printStackTrace();                    }break;            }        }    };}

3       ShowHtmlContent:

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;

public class ShowHtmlContent extends Activity{private TextView mTextView;

    @Overridepublic void onCreate(Bundle savedInstanceState)    {super.onCreate(savedInstanceState);        setContentView(R.layout.http);        mTextView = (TextView) findViewById(R.id.textview);        Intent intent = ShowHtmlContent.this.getIntent();        String dataString = intent.getStringExtra("data");if (dataString != null)        {            mTextView.setText(dataString);        }else        {            mTextView.setText("nothing to show!");        }    }}

4    效果图

     

转载于:https://www.cnblogs.com/jh5240/archive/2012/01/03/2311060.html

http get post 请求相关推荐

  1. restTemplate http请求报错:no suitable HttpMessageConverter found for response type and content type

    报错信息: org.springframework.web.client.UnknownContentTypeException: Could not extract response: no sui ...

  2. Reject: HTTP ‘DELETE‘ is not allowed, Not injecting HSTS.....DELETE请求PUT请求跨域问题

    CORS(DELETE请求.PUT请求) Reject: HTTP 'DELETE' is not allowed [DEBUG] 2021-08-25 15:23:52.401 [http-nio- ...

  3. etcd 笔记(05)— etcd 代码结构、各模块功能、整体架构、各模块之间的交互、请求和应答流程

    1. etcd 项目结构和功能 etcd 项目代码的目录结构如下: $ tree ├── auth ├── build ├── client ├── clientv3 ├── contrib ├── ...

  4. Go 学习笔记(78)— Go 标准库 net/http 创建服务端(接收 GET、POST 请求)

    使用 net/http 标准库创建一个 http 的 restful api 的服务端,用来处理 GET.POST 等请求. 源代码如下: package mainimport ("enco ...

  5. Go 学习笔记(76)— Go 标准库 net/http 创建客户端(发送 GET、POST 请求)

    1. Get 请求 1.1 使用 net/http 包的快捷方法 GET package mainimport ("fmt""io/ioutil""n ...

  6. 记录一次http请求失败的问题分析

    问题背景 当前我有一个基于Flask编写的Restful服务,由于业务的需求,我需要将该服务打包成docker 镜像进行离线部署,原始服务的端口是在6661端口进行开启,为了区分,在docker中启动 ...

  7. 前端Vue学习之路(四)axios请求数据

    axios 1.增加新知识 2.旧方案 3.新方案(一) 4.为什么要用拦截器 (新方案二) 1.增加新知识 假如每个组件都引用axios,后期如果axios库不再维护了,那每个组件都要改动 所以封装 ...

  8. 【JavaWeb】servlet与http请求协议

    Servlet: 概念: server applet (服务端小程序)运行在服务器端的小程序 Servlet就是一个接口,定义了Java类被浏览器访问到(Tomcat识别)的规则. 将我我们自定义一个 ...

  9. curl模拟post请求

    另外可尝试 postman工具 或者用request 直接请求 CURL 发送POST请求curl -header "Content-Type: application/json" ...

  10. flask_模拟请求post,get

    #coding:utf-8 import requestsres = requests.post(url="http://192.168.135.105:8888/",data={ ...

最新文章

  1. map area 鼠标跟随
  2. heartbeat 日志分析
  3. Spring AOP自动创建代理 和 ProxyFactoryBean创建代理
  4. 原生ajax的post方法,原生js实现ajax及get post方法
  5. 96 年美女胜出!那个有关“猪脸识别”的比赛决出冠军啦
  6. 如何通俗易懂地解释卷积?(2)
  7. 梨花带雨html音乐播放器源码,梨花带雨网页悬浮音乐播放器V3开源
  8. Sublime Text 2安装图解
  9. 干掉visio,这是一款免费又好用的画图神器
  10. 企业设计图纸 无纸化图纸管理方案
  11. 前端度分秒与经纬度互转
  12. DM7在linux下部署
  13. SoX — 音频处理工具里的瑞士军刀
  14. STM32(八)W25Q(16/32/64/128)芯片学习总结
  15. 计算机d盘可以格式化吗,电脑d盘格式化对电脑有影响吗
  16. jy-12-SPRINGMYBATIS02——云笔记10-刘苍松
  17. java基本类型与包装类型
  18. Github无法访问的解决方法
  19. Lind.DDD.Domain领域模型介绍
  20. Sherlock视觉,支持任意相机,我们是专业Sherlock视觉 sherlock支持二次开发,同时也支持 VB,VC,C#的界面封装

热门文章

  1. python的pandas库内的函数_python 中NumPy和Pandas工具包中的函数使用笔记(方便自己查找)...
  2. android 响应点击事件,Android响应事件onClick方法的五种实现方式小结
  3. 00截断上传绕过_【文件上传与解析】文件上传与解析漏洞总结v1.0
  4. python batch normalization_Batch Normalization 引出的一系列问题
  5. 控制div的大小自适应_可以漂移的电动轮椅,采用“自适应重心控制系统”,根本不怕翻车...
  6. 19、计算机图形学——蒙特卡洛路径追踪
  7. C++知识点44——类的继承概述
  8. 记录一下mathtype输入任意形式矩阵
  9. redis cli 删除key 模糊_redis 常用函数
  10. HPAIC人类蛋白质图谱分类挑战赛金牌经验分享