标签:android网络访问 http访问 网路通信 httpurlconnection android下http通信
在Android开发中,网络请求必然是必不可少。一般而言,都是基于http的网络请求。有时候也会有SOCKET请求,这个后续的专题再讲。今天,我们就先讲讲常用的Http请求。
http求情自然是遵循http协议的,相关内容请转接:Java学习笔记之Http协议详解
好了,开始今天的正题。
URL url = new URL( "http://192.168.31.144:10010/MINATest/servlet/DataTestServlet?username=victor&password=strikefreedom"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(TIME); conn.setReadTimeout(TIME); int responseCode = conn.getResponseCode(); if (responseCode == 200) { input = conn.getInputStream(); if (input != null) { //拿到流后处理 } }
String data = "username=justice&password=infiniteJustice"; URL url = new URL( "http://192.168.31.144:10010/MINATest/servlet/DataTestServlet"); conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(TIME); conn.setReadTimeout(TIME); conn.setDoInput(true);// 允许输入 conn.setDoOutput(true);// 允许输出 conn.setUseCaches(false);// 不使用Cache conn.setRequestProperty("Charset", ENCODING); conn.setRequestProperty("Content-Length", String.valueOf(data.length())); conn.setRequestProperty("Content-Type", "text/*;charset=utf-8"); conn.setRequestMethod("POST"); DataOutputStream outStream = new DataOutputStream( conn.getOutputStream()); outStream.write(data.getBytes()); outStream.flush(); outStream.close(); if (conn == null) { return; } int responseCode = conn.getResponseCode(); if (responseCode == 200) { input = conn.getInputStream(); if (input != null) { } }
package com.example.nettest; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.SocketException; import java.net.SocketTimeoutException; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.text.TextUtils; import android.util.Log; /** * @ClassName: FreedomHttpUrlUtils * @author victor_freedom (x_freedom_reddevil@126.com) * @createddate 2015-1-22 下午1:43:58 * @Description: http请求管理者 */ public class FreedomHttpUrlUtils implements Runnable { private Context context; /** http访问结果监听器 */ private FreedomHttpListener listener; /** 当前访问线程 */ private Thread currentRequest = null; /** 访问链接 */ HttpURLConnection conn = null; /** 拿到的流 */ InputStream input = null; private static final String ENCODING = "UTF-8"; public static final int GET_MOTHOD = 1; private static final int TIME = 40 * 1000; public static final int POST_MOTHOD = 2; /** * 1: get请求 2: post请求 */ private int requestStatus = 1; /** * <p> * Title: * </p> * <p> * Description:构造方法,其实在这里可以传入一个传输协议包,博主是测试代码,所以请求中直接写死了。 * </p> * * @param mContext * @param listener * 监听器 * @param mRequeststatus * 请求方式 */ public FreedomHttpUrlUtils(Context mContext, FreedomHttpListener listener, int mRequeststatus) { this.context = mContext; this.requestStatus = mRequeststatus; this.listener = listener; } /** * @Title: postRequest * @Description:Post请求触发 * @throws */ public void postRequest() { requestStatus = 2; currentRequest = new Thread(this); currentRequest.start(); } /** * @Title: getRequeest * @Description:GET请求触发 * @throws */ public void getRequeest() { requestStatus = 1; currentRequest = new Thread(this); currentRequest.start(); } /** * 对请求的字符串进行编码 * * @return * @throws UnsupportedEncodingException */ public static String requestEncodeStr(String requestStr) throws UnsupportedEncodingException { return URLEncoder.encode(requestStr, ENCODING); } /** * @Title: sendGetRequest * @Description: 发送get请求 * @throws */ private void sendGetRequest() { try { URL url = new URL( "http://192.168.31.144:10010/MINATest/servlet/DataTestServlet?username=victor&password=strikefreedom"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(TIME); conn.setReadTimeout(TIME); int responseCode = conn.getResponseCode(); if (responseCode == 200) { input = conn.getInputStream(); if (input != null) { listener.action(FreedomHttpListener.EVENT_GET_DATA_SUCCESS, readStream(input)); } } else { listener.action(FreedomHttpListener.EVENT_NETWORD_EEEOR, null); } } catch (SocketException e) { e.printStackTrace(); listener.action(FreedomHttpListener.EVENT_CLOSE_SOCKET, null); } catch (SocketTimeoutException e) { e.printStackTrace(); listener.action(FreedomHttpListener.EVENT_NETWORD_EEEOR, null); } catch (IOException e) { e.printStackTrace(); listener.action(FreedomHttpListener.EVENT_GET_DATA_EEEOR, null); } catch (Exception e) { e.printStackTrace(); listener.action(FreedomHttpListener.EVENT_NETWORD_EEEOR, null); } } /** * @Title: sendPostRequest * @Description: 发送post请求 * @throws */ private void sendPostRequest() { try { String data = "username=justice&password=infiniteJustice"; URL url = new URL( "http://192.168.31.144:10010/MINATest/servlet/DataTestServlet"); conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(TIME); conn.setReadTimeout(TIME); conn.setDoInput(true);// 允许输入 conn.setDoOutput(true);// 允许输出 conn.setUseCaches(false);// 不使用Cache conn.setRequestProperty("Charset", ENCODING); conn.setRequestProperty("Content-Length", String.valueOf(data.length())); conn.setRequestProperty("Content-Type", "text/*;charset=utf-8"); conn.setRequestMethod("POST"); DataOutputStream outStream = new DataOutputStream( conn.getOutputStream()); outStream.write(data.getBytes()); outStream.flush(); outStream.close(); if (conn == null) { return; } int responseCode = conn.getResponseCode(); if (responseCode == 200) { input = conn.getInputStream(); if (input != null) { listener.action(FreedomHttpListener.EVENT_GET_DATA_SUCCESS, readStream(input)); } } else if (responseCode == 404) { input = conn.getErrorStream(); if (input != null) { listener.action(FreedomHttpListener.EVENT_GET_DATA_SUCCESS, readStream(input)); } else { listener.action(FreedomHttpListener.EVENT_NETWORD_EEEOR, null); } } else { listener.action(FreedomHttpListener.EVENT_NETWORD_EEEOR, null); } } catch (SocketException e) { e.printStackTrace(); listener.action(FreedomHttpListener.EVENT_CLOSE_SOCKET, null); } catch (SocketTimeoutException e) { e.printStackTrace(); listener.action(FreedomHttpListener.EVENT_NETWORD_EEEOR, null); } catch (IOException e) { e.printStackTrace(); listener.action(FreedomHttpListener.EVENT_GET_DATA_EEEOR, null); } catch (Exception e) { e.printStackTrace(); listener.action(FreedomHttpListener.EVENT_NETWORD_EEEOR, null); } } /** * @Title: isRunning * @Description: 判断是否正在访问 * @return * @throws */ public boolean isRunning() { if (currentRequest != null && currentRequest.isAlive()) { return true; } return false; } /** * 读取数据 * * @param inStream * 输入流 * @return * @throws Exception */ private Object readStream(InputStream inStream) throws Exception { String result; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } result = new String(outStream.toByteArray(), ENCODING); outStream.close(); inStream.close(); return result; } /** * 取消当前HTTP连接处理 */ public void cancelHttpRequest() { if (currentRequest != null && currentRequest.isAlive()) { if (input != null) { try { input.close(); } catch (Exception e) { e.printStackTrace(); } } input = null; if (conn != null) { try { conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } } conn = null; currentRequest = null; System.gc(); } } /** * 发送请求 */ public void run() { // 判断是否有网络 boolean netType = NetUtils.checkNetWork(context); if (netType) { if (requestStatus == 1) { sendGetRequest(); } else if (requestStatus == 2) { sendPostRequest(); } } else { listener.action(FreedomHttpListener.EVENT_NOT_NETWORD, null); } } }
package com.example.nettest; /** * @ClassName: FreedomHttpListener * @author victor_freedom (x_freedom_reddevil@126.com) * @createddate 2015-1-24 下午4:28:31 * @Description: 监听器 */ public interface FreedomHttpListener { public static final int EVENT_BASE = 0x100; /** * 没有网络的信息提示 * */ public static final int EVENT_NOT_NETWORD = EVENT_BASE + 1; /** * 网络异常的信息提示 * */ public static final int EVENT_NETWORD_EEEOR = EVENT_BASE + 2; /** * 获取网络数据失败 * */ public static final int EVENT_GET_DATA_EEEOR = EVENT_BASE + 3; /** * 获取网络数据成功 * */ public static final int EVENT_GET_DATA_SUCCESS = EVENT_BASE + 4; /** * 获取网络数据成功 * */ public static final int EVENT_CLOSE_SOCKET = EVENT_BASE + 5; public void action(int actionCode, Object object); }
/** * @ClassName: BaseHandler * @author victor_freedom (x_freedom_reddevil@126.com) * @createddate 2015-1-24 下午4:32:05 * @Description: 消息处理器 */ class BaseHandler extends Handler { private Context context; /** 事件回调接口处理 */ private FreedomDataCallBack callBack; public BaseHandler(Context context, FreedomDataCallBack callBack) { this.context = context; this.callBack = callBack; } public void handleMessage(Message msg) { // 根据不同的结果触发不同的动作 if (msg.what == FreedomHttpListener.EVENT_GET_DATA_SUCCESS) { if (msg.obj == null) { callBack.onFailed(); } else { // 后台处理数据 callBack.processData(msg.obj, true); } } else if (msg.what == FreedomHttpListener.EVENT_NOT_NETWORD) { callBack.onFailed(); // CommonUtil.showInfoDialog(context, // getString(R.string.net_error)); } else if (msg.what == FreedomHttpListener.EVENT_NETWORD_EEEOR) { callBack.onFailed(); } else if (msg.what == FreedomHttpListener.EVENT_GET_DATA_EEEOR) { callBack.onFailed(); } else if (msg.what == FreedomHttpListener.EVENT_CLOSE_SOCKET) { } callBack.onFinish(); } }
package com.example.nettest; /** * @ClassName: FreedomDataCallBack * @author victor_freedom (x_freedom_reddevil@126.com) * @createddate 2015-1-24 下午4:33:38 * @Description: 回调接口,处理返回数据 * @param <T> */ public interface FreedomDataCallBack<T> { public abstract void onStart(); public abstract void processData(T paramObject, boolean paramBoolean); public abstract void onFinish(); public abstract void onFailed(); }
/** * @Title: getDataFromServer * @Description: 从服务器拿数据 * @param requestType * 请求方式 * @param callBack * 回调接口 * @throws */ protected void getDataFromServer(int requestType, FreedomDataCallBack callBack) { final BaseHandler handler = new BaseHandler(this, callBack); freedomHttpUrlUtils = new FreedomHttpUrlUtils(mContext, new FreedomHttpListener() { @Override public void action(int actionCode, Object object) { Message msg = new Message(); switch (actionCode) { case FreedomHttpListener.EVENT_NOT_NETWORD: msg.what = FreedomHttpListener.EVENT_NOT_NETWORD; break; case FreedomHttpListener.EVENT_NETWORD_EEEOR: msg.what = FreedomHttpListener.EVENT_NETWORD_EEEOR; break; case FreedomHttpListener.EVENT_CLOSE_SOCKET: msg.what = FreedomHttpListener.EVENT_CLOSE_SOCKET; break; case FreedomHttpListener.EVENT_GET_DATA_EEEOR: msg.what = FreedomHttpListener.EVENT_GET_DATA_EEEOR; msg.obj = null; break; case FreedomHttpListener.EVENT_GET_DATA_SUCCESS: msg.obj = object; msg.what = FreedomHttpListener.EVENT_GET_DATA_SUCCESS; break; default: break; } handler.sendMessage(msg); } }, requestType); callBack.onStart(); // 选择不同的请求方法 if (requestType == FreedomHttpUrlUtils.GET_MOTHOD) { freedomHttpUrlUtils.getRequeest(); } else if (requestType == FreedomHttpUrlUtils.POST_MOTHOD) { freedomHttpUrlUtils.postRequest(); } }
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String parameter = request.getParameter("username"); String parameter2 = request.getParameter("password"); System.out.println(parameter + parameter2); response.setContentType("text/*;charset=utf-8"); response.getWriter().write(parameter + "-" + parameter2); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // doGet(request, response); System.out.println("post"); ServletInputStream inputStream = request.getInputStream(); ByteOutputStream b = new ByteOutputStream(); int len = -1; byte[] buf = new byte[1024]; while ((len = inputStream.read(buf)) != -1) { b.write(buf, 0, buf.length); } String s = b.toString(); String[] split = s.split("&"); System.out.println(split[0] + split[1]); response.setContentType("text/*;charset=utf-8"); response.getWriter().write( split[0].substring(split[0].lastIndexOf("=") + 1) + "-" + split[1].substring(split[1].lastIndexOf("=") + 1)); }
/** * @ClassName: MainActivity * @author victor_freedom (x_freedom_reddevil@126.com) * @createddate 2015-1-24 下午4:40:59 * @Description: TODO */ public class MainActivity extends BaseActivity { private TextView get; private TextView post; @Override protected void findViewById() { get = (TextView) findViewById(R.id.hello); post = (TextView) findViewById(R.id.post); } @Override protected void loadViewLayout() { setContentView(R.layout.activity_main); } @Override protected void processLogic() { } @Override protected void setListener() { } @Override protected void init() { getDataFromServer(1, new FreedomDataCallBack<String>() { @Override public void onStart() { } @Override public void processData(String paramObject, boolean paramBoolean) { get.setText(paramObject); } @Override public void onFinish() { } @Override public void onFailed() { } }); getDataFromServer(2, new FreedomDataCallBack<String>() { @Override public void onStart() { } @Override public void processData(String paramObject, boolean paramBoolean) { post.setText(paramObject); } @Override public void onFinish() { } @Override public void onFailed() { } }); }
Android开发之网络请求通信专题(一):基于HttpURLConnection的请求通信
标签:android网络访问 http访问 网路通信 httpurlconnection android下http通信
原文地址:http://blog.csdn.net/victorfreedom/article/details/43086435