android 怎么使用httpurlconnection
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 怎么使用httpurlconnection相关的知识,希望对你有一定的参考价值。
参考技术A 参考如下内容:HttpURLConnection vs DefaultHttpClient
在android API Level 9(Android 2.2)之前之能使用DefaultHttpClient类发送http请求。DefaultHttpClient是Apache用于发送http请求的客户端,其提供了强大的API支持,而且基本没有什么bug,但是由于其太过复杂,Android团队在保持向后兼容的情况下,很难对DefaultHttpClient进行增强。为此,Android团队从Android API Level 9开始自己实现了一个发送http请求的客户端类——–HttpURLConnection。
相比于DefaultHttpClient,HttpURLConnection比较轻量级,虽然功能没有DefaultHttpClient那么强大,但是能够满足大部分的需求,所以Android推荐使用HttpURLConnection代替DefaultHttpClient,并不强制使用HttpURLConnection。
但从Android API Level 23(Android 6.0)开始,不能再在Android中使用DefaultHttpClient,强制使用HttpURLConnection。
测试微信公众平台图文统计接口
package com;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
public class testTongji {
//获取token
public static String accesstoken=null;
static {
try{
accesstoken=getToken();
}catch(Exception e){
e.printStackTrace();
}
}
public static String getToken ()throws Exception{
String userName="getTokenUser";
String password="123456";
String address="http://192.169.1.47/:8081/szset/weix_getToken.htm";
String param="userName="+userName+"&password="+password;
String Strjson=httpPost(address,param);
Map<String,String> mapst=toMap(Strjson);
String token=mapst.get("token");
System.out.println(token);
return token;
}
//httpPost请求
public static String httpPost (String address,String param)throws Exception{
URL url = null;
HttpURLConnection urlConn = null;
url = new URL(address);
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setDoOutput(true);
OutputStream out = urlConn.getOutputStream();
if(param!=null&&!"".equals(param)){
out.write(param.toString().getBytes("utf-8"));
}
out.flush();
out.close();
StringBuffer str = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"utf-8"));
String inputLine = null;
while ( (inputLine = in.readLine()) != null){
str.append(inputLine);
}
in.close();
return str.toString();
}
/**
* 将Json对象转换成Map
*
* @param jsonObject
* json对象
* @return Map对象
* @throws JSONException
*/
public static Map<String,String> toMap(String jsonString) throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject=JSONObject.fromObject(jsonString);
Map<String,String> result = new HashMap<String,String>();
Iterator iterator = jsonObject.keys();
String key = null;
String value = null;
while (iterator.hasNext()) {
key = (String) iterator.next();
value = jsonObject.getString(key);
result.put(key, value);
}
return result;
}
//测试统计数据
public static void testTJ() throws Exception{
//https://api.weixin.qq.com/datacube/getarticlesummary?access_token=ACCESS_TOKEN
String address="https://api.weixin.qq.com/datacube/getarticlesummary?";
String ACCESS_TOKEN=accesstoken;
address=address+"access_token="+ACCESS_TOKEN;
/**
* {
"begin_date": "2014-12-08",
"end_date": "2014-12-08"
}
*/
//拼接参数
StringBuffer strbuf=new StringBuffer();
String begin_date = "2014-12-08";
String end_date = "2015-12-08";
strbuf.append("{");
strbuf.append("\"begin_date\":\"").append(begin_date).append("\",");
strbuf.append("\"end_date\":\"").append(end_date).append("\"" );
strbuf.append("}");
String param=strbuf.toString();
String Strjson=httpPost(address,param);
System.out.println("原始数据:");
System.out.println(Strjson.toString());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer strbuf=new StringBuffer();
String begin_date = "2014-12-08";
String end_date = "2015-12-08";
strbuf.append("{");
strbuf.append("\"begin_date\":\"").append(begin_date).append("\",");
strbuf.append("\"end_date\":\"").append(end_date).append("\"" );
strbuf.append("}");
String param=strbuf.toString();
System.out.println("param:"+param);
}
}
以上是关于android 怎么使用httpurlconnection的主要内容,如果未能解决你的问题,请参考以下文章