论坛首页 移动开发技术论坛

Android使用HttpURLConnection提交数据

浏览 4765 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-10-10   最后修改:2009-10-10
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 {

@Override
public 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... ]");
}
}
 

 

论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics