Android使用HttpURLConnection提交数据
16lz
2022-04-02
import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.Properties;import android.util.Log;import com.xxx.may.api.ASendManager;public class HTTPSend extends ASendManager {@Overridepublic void sendData(Properties paratemerproperties) {String tag ="HTTPSend:sendData";int statsID = 101;String s = "TYPE=101,102,103,104,105,100&NAME=1,2,3,4,5,6&VALUE=clicknews,2009-06-11-22:33:11,320,nomessageforyou,1000,NullPointException&IMEI=10001&APPID=91";String url1 = (String) paratemerproperties.get("post_data_url");Log.e(tag, "send the data:"+url1+s);try {URL url = new URL(url1);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setDoOutput(true);connection.setRequestMethod("POST");connection.setReadTimeout(20000);connection.setConnectTimeout(20000);OutputStream out = connection.getOutputStream();out.write(s.getBytes());out.flush();out.close();Log.e(tag, "Response code :"+connection.getResponseCode());connection.disconnect();} catch (Exception e) {e.printStackTrace();}}}/*** <p>* Resend the data when you want to* </p>* * @param data* data* @throws IOException* {@link IOException}*/private void resend(String datas[], String url,String appid) throws IOException {String tag = "HTTPSend:resend";DefaultHttpClient dhc = new DefaultHttpClient();HttpPost hp = new HttpPost(url1);HttpResponse httpResponse = null;List<NameValuePair> list = new ArrayList<NameValuePair>();list.add(new BasicNameValuePair(ParameterConstanst.TIME_API_SEND, System.currentTimeMillis()+ ""));list.add(new BasicNameValuePair(ParameterConstanst.APPLICATION_ID, appid));list.add(new BasicNameValuePair(ParameterConstanst.OS, "android"));list.add(new BasicNameValuePair(ParameterConstanst.PLATFORM, "1.5"));list.add(new BasicNameValuePair(ParameterConstanst.MSISDN, "000000000"));list.add(new BasicNameValuePair(ParameterConstanst.PROTOCOL_VERSION, "1"));list.add(new BasicNameValuePair(ParameterConstanst.IMEI, CollectIMEI.getIMEI()));list.add(new BasicNameValuePair(ParameterConstanst.TIME_API_CALLED, datas[0]));list.add(new BasicNameValuePair(ParameterConstanst.STATS_ID, datas[1].trim()));list.add(new BasicNameValuePair(ParameterConstanst.STATS_VALUE, datas[2].trim()));try {hp.setEntity(new UrlEncodedFormEntity(list));hp.setHeader(new BasicHeader(ParameterConstanst.TIME_OUT, "20000"));httpResponse = dhc.execute(hp);LogAPI.i(tag, "send data result : "+ httpResponse.getStatusLine().getStatusCode());} catch (ClientProtocolException e1) {LogAPI.e(tag, "[ Exception:+"+e1.getMessage()+" ]");} catch (IOException e1) {LogAPI.e(tag, "[ Exception:+"+e1.getMessage()+" ]");throw e1;}finally{hp.abort();LogAPI.e(tag, "[ finally in send data... ]");}}
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer