Android怎样在http头信息里设置参数

Android怎样在http头信息里设置参数,第1张

在使用http请求server时常常要传递一些参数给server,如IMEI号、平台号、渠道号、客户端的版本号等一些通用信息,像这些参数我们没有必要每次都拼在url后,我们可以统一添加到http头里。

1.HttpClient的设置http头的参数

HttpClient httpclient = new DefaultHttpClient()

httpclient.getParams().setParameter(

CoreConnectionPNames.CONNECTION_TIMEOUT, CONN_TIME_OUT)

HttpGet httpget = new HttpGet(url)

httpget.addHeader("version", SystemInfo.getVersionChars())

httpget.addHeader("client_token", SystemInfo.getIMEI())

httpget.addHeader("platform", SystemInfo.getPlatForm() + "")

httpget.addHeader("channel_id", SystemInfo.getChannelId() + "")

2.HttpURLConnection的设置http头的参数

httpURLConnection.addRequestProperty("version",

SystemInfo.getVersionChars())

httpURLConnection.addRequestProperty("client_token",

SystemInfo.getIMEI())

httpURLConnection.addRequestProperty("platform",

SystemInfo.getPlatForm() + "")

httpURLConnection.addRequestProperty("channel_id",

SystemInfo.getChannelId() + "")

一般传输参数使用json类型或者map类型都是使用post方法。

使用json数据格式发送信息向服务器端:

HttpClient httpClient = new DefaultHttpClient()

try {

HttpPost httpPost = new HttpPost(BASIC_URL + url)

List<NameValuePair>nameValuePair = new ArrayList<NameValuePair>()

JSONObject jsonObject = new JSONObject()

JSONObject jsonObject2 = new JSONObject()

jsonObject.put("uemail", userbean.getEmail())

jsonObject.put("password", userbean.getPassword())

jsonObject2.put("userbean", jsonObject)

nameValuePair.add(new BasicNameValuePair("jsonString", jsonObject

.toString()))

Log.i("lifeweeker", jsonObject2.toString())

httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair))


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11421092.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存