4.安卓中get和post的简单实现

来源:互联网 发布:vue安装sass淘宝镜像 编辑:程序博客网 时间:2024/05/29 17:10

开发过web的就知道现在的网络编程无疑是基于tcp或者udp,这些都是层层网络协议包装后的结果,例如底层的有can协议,485,232协议等等

例子是也是来自疯狂安卓讲义里的

get方式的请求

public static String sendGet(String url, String params) {String result = "";BufferedReader in = null;try {String urlName = url + "?" + params;URL realUrl = new URL(urlName);// 打开和URL之间的连接URLConnection conn = realUrl.openConnection();// 设置通用的请求属性conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");// 建立实际的连接conn.connect(); // ①// 获取所有响应头字段Map<String, List<String>> map = conn.getHeaderFields();// 遍历所有的响应头字段for (String key : map.keySet()) {System.out.println(key + "--->" + map.get(key));}// 定义BufferedReader输入流来读取URL的响应in = new BufferedReader(new InputStreamReader(conn.getInputStream()));String line;while ((line = in.readLine()) != null) {result += "\n" + line;}} catch (Exception e) {System.out.println("发送GET请求出现异常!" + e);e.printStackTrace();}// 使用finally块来关闭输入流finally {try {if (in != null) {in.close();}} catch (IOException ex) {ex.printStackTrace();}}return result;}
post方式请求

public static String sendPost(String url, String params) {PrintWriter out = null;BufferedReader in = null;String result = "";try {URL realUrl = new URL(url);// 打开和URL之间的连接URLConnection conn = realUrl.openConnection();// 设置通用的请求属性conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");// 发送POST请求必须设置如下两行conn.setDoOutput(true);conn.setDoInput(true);// 获取URLConnection对象对应的输出流out = new PrintWriter(conn.getOutputStream());// 发送请求参数out.print(params); // ②// flush输出流的缓冲out.flush();// 定义BufferedReader输入流来读取URL的响应in = new BufferedReader(new InputStreamReader(conn.getInputStream()));String line;while ((line = in.readLine()) != null) {result += "\n" + line;}} catch (Exception e) {System.out.println("发送POST请求出现异常!" + e);e.printStackTrace();}// 使用finally块来关闭输出流、输入流finally {try {if (out != null) {out.close();}if (in != null) {in.close();}} catch (IOException ex) {ex.printStackTrace();}}return result;}

package org.crazyit.net;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;/** * Description: * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author  Yeeku.H.Lee kongyeeku@163.com * @version  1.0 */public class GetPostMain extends Activity{Button get , post;TextView show;// 代表服务器响应的字符串String response;Handler handler = new Handler(){@Overridepublic void handleMessage(Message msg){if(msg.what == 0x123){// 设置show组件显示服务器响应show.setText(response);}}};@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);get = (Button) findViewById(R.id.get);post = (Button) findViewById(R.id.post);show = (TextView)findViewById(R.id.show);get.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){new Thread(){@Overridepublic void run(){response = GetPostUtil.sendGet("http://192.168.1.88:8888/abc/a.jsp", null);// 发送消息通知UI线程更新UI组件handler.sendEmptyMessage(0x123);}}.start();}});post.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){new Thread(){@Overridepublic void run(){response = GetPostUtil.sendPost("http://192.168.1.88:8888/abc/login.jsp", "name=crazyit.org&pass=leegang");}}.start();// 发送消息通知UI线程更新UI组件handler.sendEmptyMessage(0x123);}});}}



0 0
原创粉丝点击