Android之Http请求基础
16lz
2022-04-24
一、使用HttpGet方式进行请求
private Button myButton = null;private HttpResponse httpResponse = null;private HttpEntity httpEntity = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myButton = (Button) findViewById(R.id.myButton);myButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stub//生成一个请求对象//参数:请求的地址HttpGet httpGet = new HttpGet("http://172.16.3.102:8080/Ibeacon/NewFile.jsp");//生成一个http客户端对象HttpClient httpClient = new DefaultHttpClient();InputStream inputStream = null;try {try {//获取请求的响应httpResponse = httpClient.execute(httpGet);//从响应当中获取数据流httpEntity = httpResponse.getEntity();inputStream = httpEntity.getContent();BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputStream));//保存获取到的数据String result = "";String line = "";while ((line = bufferedreader.readLine()) != null) {result += line;}System.out.println(result);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}).start();}});}
二、使用HttpPost方式进行请求
private Button myButton = null;//声明请求的链接private String myURL = "http://172.16.3.102:8080/Ibeacon/Receive";HttpResponse httpResponse = null;HttpEntity httpEntity = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myButton = (Button) findViewById(R.id.myButton);myButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stub//键值对//用来设置请求的参数NameValuePair nameValuePair1 = new BasicNameValuePair("uuid", "300001");NameValuePair nameValuePair2 = new BasicNameValuePair("classify", "200001");List<NameValuePair> lists = new ArrayList<NameValuePair>();lists.add(nameValuePair1);lists.add(nameValuePair2);try {//HttpEntity既可以是请求体也可以是响应体//设置请求的参数HttpEntity requesthttpEntity = new UrlEncodedFormEntity(lists);//生成一个请求Post请求对象HttpPost httppost = new HttpPost(myURL);//设置请求体httppost.setEntity(requesthttpEntity);//生成一个http客户端对象HttpClient httpclient = new DefaultHttpClient();InputStream inputStream = null;BufferedReader reader = null;try {//获取请求响应httpResponse = httpclient.execute(httppost);//获取数据httpEntity = httpResponse.getEntity();inputStream = httpEntity.getContent();reader = new BufferedReader(new InputStreamReader(inputStream));//保存数据String result = "";String line = "";while ((line = reader.readLine()) != null) {result += line;}// System.out.println(result);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();} finally {try {inputStream.close();reader.close();} catch (Exception e2) {// TODO: handle exception}}} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}).start();}});}
更多相关文章
- android 获取包名 通过包名获取应用签名
- Android中音频文件的使用
- Android(安卓)你的OnTouchEvent() 触屏事件的优化了吗
- [置顶] android调用.net的webservice的例子获取手机的归属地
- Android(安卓)反射获取私有方法,成员变量
- android 检测应用程序信息
- 获取android手机信息
- android 源码获取
- Android常用代码