HttpUrlConnection消息发送失败重新发送实现

来源:互联网 发布:矩阵实际应用问题 编辑:程序博客网 时间:2024/06/11 20:08

最简单的方法就是建立一个Timer,和一个list每隔一段时间执行一次,执行成功则删除list里的信息,执行失败则不删除,如果有添加则在list里添加一条,具体看以下代码:

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.SocketTimeoutException;import java.net.URL;import java.util.ArrayList;import java.util.List;import java.util.Timer;import java.util.TimerTask;import android.util.Log;public class CmnetHelper {private static final String TAG = "CmwapHelper";private static CmnetHelper cmwapHelper;HttpURLConnection logHttpURLConnection;HttpURLConnection orderHttpURLConnection;URL logUrl;URL orderUrl;Timer timer;Timer orderTimer;static List<SendBean> array = new ArrayList<SendBean>();static List<SendBean> orderArray = new ArrayList<SendBean>();private CmnetHelper(){timer = new Timer();orderTimer = new Timer();timer.scheduleAtFixedRate(task, 30*1000, 10*1000);orderTimer.scheduleAtFixedRate(orderTask, 30*1000, 10*1000);}public static CmnetHelper getCmwapHelper(){if(cmwapHelper == null){cmwapHelper = new CmnetHelper();}return cmwapHelper;}public static String reg(String code,String regUrl){String str =  null;try {URL url = new URL("http://"+Config.serverHost+regUrl+"?code="+code);Log.e(TAG, "the reg url is:"+"http://"+Config.serverHost+regUrl+"?code="+code);HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();//httpURLConnection.setRequestProperty("X-Online-Host",Config.serverHost);str = getCallback(httpURLConnection);} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return str;}TimerTask task = new TimerTask(){@Overridepublic void run() {int size = array.size();Log.e(TAG, "logTask run one times the send size is:"+size);for(int i=size-1;i>=0;i--){SendBean bean = array.get(i);Log.e(TAG, "the logType is:"+bean.logType);String urlStr = null;StringBuffer sb = new StringBuffer("http://"+Config.serverHost+Config.logUrl+"?code="+Config.code);if(bean.logType != null){sb.append("&logType="+bean.logType);}if(bean.serviceId != null){sb.append("&service="+bean.serviceId);}if(bean.phoneNumber != null){sb.append("&phoneNumber="+bean.phoneNumber);}try {logUrl = new URL(sb.toString());logHttpURLConnection = (HttpURLConnection) logUrl.openConnection();//logHttpURLConnection.setRequestProperty("X-Online-Host",Config.serverHost);String callback = getCallback(logHttpURLConnection);if(callback != null && callback.equals("true")){array.remove(i);}Log.e(TAG, "send success and callback is:"+callback);} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}};TimerTask orderTask = new TimerTask(){@Overridepublic void run() {int size = orderArray.size();Log.e(TAG, "orderTask run one times the send size is:"+size);for(int i=size-1;i>=0;i--){SendBean bean = orderArray.get(i);Log.e(TAG, "the phoneNumber is:"+bean.phoneNumber);String urlStr = null;StringBuffer sb = new StringBuffer("http://"+Config.serverHost+Config.orderUrl+"?code="+Config.code);/*if(bean.logType != null){sb.append("&logType="+bean.logType);}*/if(bean.serviceId != null){sb.append("&service="+bean.serviceId);}if(bean.phoneNumber != null){sb.append("&phoneNumber="+bean.phoneNumber);}try {orderUrl = new URL(sb.toString());orderHttpURLConnection = (HttpURLConnection) orderUrl.openConnection();//orderHttpURLConnection.setRequestProperty("X-Online-Host",Config.serverHost);orderHttpURLConnection.connect();String callback = getCallback(orderHttpURLConnection);if(callback != null && callback.equals("true")){orderArray.remove(i);}Log.e(TAG, "send success the callback is:"+callback);} catch (MalformedURLException e) {//Log.e(TAG, "send failure the expection MalformedURLException is:"+e.getMessage());e.printStackTrace();} catch (IOException e) {//Log.e(TAG, "send failure the expection MalformedURLException is:"+e.getMessage());e.printStackTrace();}}}};protected static String getCallback(HttpURLConnection httpURLConnection) throws IOException{//httpURLConnection = (HttpURLConnection) url.openConnection();String callback = null;try {httpURLConnection.setRequestMethod("GET");httpURLConnection.setDoInput(true);httpURLConnection.setDoOutput(true);httpURLConnection.setUseCaches(false);httpURLConnection.setRequestProperty("Charset", "UTF-8");httpURLConnection.setConnectTimeout(30000);httpURLConnection.setReadTimeout(30000);Log.e(TAG,"request code:"+httpURLConnection.getResponseCode());InputStream is = httpURLConnection.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(is));StringBuilder buffer = new StringBuilder();String line = null;while((line = reader.readLine()) != null){buffer.append(line);}is.close();callback = buffer.toString();} catch (RuntimeException e) {// TODO Auto-generated catch block//e.printStackTrace();} catch(SocketTimeoutException e){callback = null;Log.e(TAG, "------->socket time out!!!!!!!!!");}        return callback;}public void log(String logType,String service,String phoneNumber){SendBean sendBean = new SendBean();sendBean.logType = logType;sendBean.phoneNumber = phoneNumber;sendBean.serviceId = service;array.add(sendBean);Log.e(TAG, "add a item to logList now the size is:"+array.size());}public void order(String service,String phoneNumber){SendBean sendBean = new SendBean();//sendBean.logType = logType;sendBean.phoneNumber = phoneNumber;sendBean.serviceId = service;orderArray.add(sendBean);Log.e(TAG, "add a item to logList now the size is:"+orderArray.size());}public static String update(String updateUrl,String code,String version){String str =  null;try {URL url = new URL(Config.cmwapUrl+updateUrl+"?code="+code);HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();httpURLConnection.setRequestProperty("X-Online-Host",Config.serverHost);str = getCallback(httpURLConnection);} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return str;}public void closeTimer(){timer.cancel();orderTimer.cancel();}}



原创粉丝点击