java setcontenttype_Java HttpEntity.setContentType方法代码示例

import org.apache.http.HttpEntity; //导入方法依赖的package包/类

public String jsonPost(String str, ArrayList> arrayList,

ArrayList> arrayList2, ArrayList> arrayList3)

throws Throwable {

long currentTimeMillis = System.currentTimeMillis();

Ln.i("jsonPost: " + str, new Object[0]);

HttpUriRequest httpPost = new HttpPost(str);

StringPart stringPart = new StringPart();

if (arrayList != null) {

HashMap hashMap = new HashMap();

Iterator it = arrayList.iterator();

while (it.hasNext()) {

KVPair kVPair = (KVPair) it.next();

hashMap.put(kVPair.name, kVPair.value);

}

stringPart.append(new Hashon().fromHashMap(hashMap));

}

HttpEntity inputStreamEntity = stringPart.getInputStreamEntity();

inputStreamEntity.setContentType("application/json");

httpPost.setEntity(inputStreamEntity);

if (arrayList2 != null) {

Iterator it2 = arrayList2.iterator();

while (it2.hasNext()) {

kVPair = (KVPair) it2.next();

httpPost.setHeader(kVPair.name, (String) kVPair.value);

}

}

HttpParams basicHttpParams = new BasicHttpParams();

HttpConnectionParams.setConnectionTimeout(basicHttpParams, connectionTimeout);

HttpConnectionParams.setSoTimeout(basicHttpParams, readTimout);

if (arrayList3 != null) {

Iterator it3 = arrayList3.iterator();

while (it3.hasNext()) {

kVPair = (KVPair) it3.next();

try {

basicHttpParams.setIntParameter(kVPair.name, R.parseInt(String.valueOf(kVPair

.value)));

} catch (Exception e) {

}

}

}

httpPost.setParams(basicHttpParams);

HttpClient sSLHttpClient = str.startsWith("https://") ? getSSLHttpClient() : new

DefaultHttpClient();

HttpResponse execute = sSLHttpClient.execute(httpPost);

int statusCode = execute.getStatusLine().getStatusCode();

if (statusCode == 200 || statusCode == 201) {

String entityUtils = EntityUtils.toString(execute.getEntity(), Constants.UTF_8);

sSLHttpClient.getConnectionManager().shutdown();

Ln.i("use time: " + (System.currentTimeMillis() - currentTimeMillis), new Object[0]);

return entityUtils;

}

entityUtils = EntityUtils.toString(execute.getEntity(), Constants.UTF_8);

HashMap hashMap2 = new HashMap();

hashMap2.put("error", entityUtils);

hashMap2.put("status", Integer.valueOf(statusCode));

sSLHttpClient.getConnectionManager().shutdown();

throw new Throwable(new Hashon().fromHashMap(hashMap2));

}

以下是Java获取长期有效的access_token的代码示例: ```java class MyAccessToken { 修改为自己的app和appsecret private static final StringID = "your appid"; private static final String APPSECRET = "yoursecret"; // 获取access_token的接口 private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token"; // access_token保存时间(单位:毫秒) private static final long ACCESS_TOKEN_EXPIRE_TIME = 7200000; // 2小时 private static String access_token = null; private static long access_token_create_time = 0; /** * 获取access_token * * @return access_token字符串 */ public static String getAccessToken() { // 判断是否超过保存时间 long now_time = System.currentTimeMillis() / 1000; if (now_time - access_token_create_time > ACCESS_TOKEN_EXPIRE_TIME || access_token == null) { JSONObject json = new JSONObject(); try { json.put("grant_type", "client_credential"); json.put("appid", APPID); json.put("secret", APPSECRET); String result = HttpUtil.post(ACCESS_TOKEN_URL, json.toString()); JSONObject resultJson = new JSONObject(result); if (resultJson.has("access_token")) { access_token = resultJson.getString("access_token"); access_token_create_time = System.currentTimeMillis() / 1000; } else { throw new Exception(result); } } catch (Exception e) { System.out.println("获取access_token出现错误:" + e.getMessage()); } } return access_token; } /** * 发起HTTP请求获取返回结果 * * @param url HTTP请求URL * @param json HTTP请求body * @return 返回结果字符串 * @throws Exception */ private static String post(String url, String json) throws Exception { String result = null; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); StringEntity entity = new StringEntity(json, "utf-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); HttpResponse httpResponse = httpClient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == 200) { HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { result = EntityUtils.toString(httpEntity, "UTF-8"); } } httpClient.close(); return result; } } ``` 请注意,如果要使用上述示例代码,需要替换APPID和APPSECRET为真实的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值