对于HttpPost 方法的简单集成 加载服务器端的JsonArray 到客户端,变成List<HashMap<String,String>>
public static List<HashMap<String, String>> loadString(String path,HashMap<String, String> hash) throws Exception{
// HttpPost 访问网络 path 访问路径 hash传递的参数
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); // 传递的参数的封装
List<HashMap<String, String>> listvalue = new ArrayList<HashMap<String,String>>();//返回的数据
HttpClient client = new DefaultHttpClient();//HttpClient 客户端用于执行HttpPost 或者 HttpGet
HttpPost post = new HttpPost(path); // Post请求方法,把路径带上
if(hash!=null){
Set<String> set = hash.keySet(); //得到hashmap 的key值,
Iterator<String> it = set.iterator(); //使用迭代器把key值输出
while(it.hasNext()){
String name = it.next(); //得到key值
String value = hash.get(name);//利用key值得到value
BasicNameValuePair pair = new BasicNameValuePair(name,value); // key-value 的封装
list.add(pair);
}
HttpEntity entity = new UrlEncodedFormEntity(list);
post.setEntity(entity);
}
HttpResponse response = client.execute(post); //客户端执行封装
if(response.getStatusLine().getStatusCode()==200){
//响应成功 返回 200
HttpEntity entity1 = response.getEntity(); //得到响应的实体
String jsonarray = EntityUtils.toString(entity1);//
/**
* 利用EntityString把实体转化成String
或者可以把 用输入流的
InputStream in = entity1.getContent();
BufferedReader read = new BufferedReader(new InputStreamReader(in));
StringBuffer sb =new StringBuffer();
String str ;
while((str=read.readLine())!=null){
sb.append(str);
}
*/
JSONArray array = new JSONArray(jsonarray);// 把得到的数据转化成Jsonarray
for(int i=0;i<array.length();i++){// 利用for 循环把输出JsonObject
JSONObject object = array.getJSONObject(i);
Iterator<String> its = object.keys(); //获取JsonObject 的key值,通过key值获取value
HashMap<String, String> hash2 = new HashMap<String, String>(); //每一个JsonObject ,新建一个Hashmap对象
while(its.hasNext()){
String key = its.next();
String values = object.getString(key);
Log.i("--------------", ""+key+""+values);
hash2.put(key, values);
}
listvalue.add(hash2);// 把hashmap 加入List集合中
}
}
return listvalue;
}
HttpGet get = new HttpGet(path);
HashMap<String, String> hash ;
List<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
HttpResponse response = client.execute(get);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String jsonarray = EntityUtils.toString(entity);
JSONArray array = new JSONArray(jsonarray);
for(int i=0;i<array.length();i++){
JSONObject object = array.getJSONObject(i);
Iterator<String> it = object.keys();
hash = new HashMap<String, String>();
while(it.hasNext()){
String name = it.next();
String value = object.getString(name);
hash.put(name, value);
}
list.add(hash);
}
}
return list;
}
public static List<HashMap<String, String>> loadString(String path,HashMap<String, String> hash) throws Exception{
// HttpPost 访问网络 path 访问路径 hash传递的参数
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); // 传递的参数的封装
List<HashMap<String, String>> listvalue = new ArrayList<HashMap<String,String>>();//返回的数据
HttpClient client = new DefaultHttpClient();//HttpClient 客户端用于执行HttpPost 或者 HttpGet
HttpPost post = new HttpPost(path); // Post请求方法,把路径带上
if(hash!=null){
Set<String> set = hash.keySet(); //得到hashmap 的key值,
Iterator<String> it = set.iterator(); //使用迭代器把key值输出
while(it.hasNext()){
String name = it.next(); //得到key值
String value = hash.get(name);//利用key值得到value
BasicNameValuePair pair = new BasicNameValuePair(name,value); // key-value 的封装
list.add(pair);
}
HttpEntity entity = new UrlEncodedFormEntity(list);
post.setEntity(entity);
}
HttpResponse response = client.execute(post); //客户端执行封装
if(response.getStatusLine().getStatusCode()==200){
//响应成功 返回 200
HttpEntity entity1 = response.getEntity(); //得到响应的实体
String jsonarray = EntityUtils.toString(entity1);//
/**
* 利用EntityString把实体转化成String
或者可以把 用输入流的
InputStream in = entity1.getContent();
BufferedReader read = new BufferedReader(new InputStreamReader(in));
StringBuffer sb =new StringBuffer();
String str ;
while((str=read.readLine())!=null){
sb.append(str);
}
*/
JSONArray array = new JSONArray(jsonarray);// 把得到的数据转化成Jsonarray
for(int i=0;i<array.length();i++){// 利用for 循环把输出JsonObject
JSONObject object = array.getJSONObject(i);
Iterator<String> its = object.keys(); //获取JsonObject 的key值,通过key值获取value
HashMap<String, String> hash2 = new HashMap<String, String>(); //每一个JsonObject ,新建一个Hashmap对象
while(its.hasNext()){
String key = its.next();
String values = object.getString(key);
Log.i("--------------", ""+key+""+values);
hash2.put(key, values);
}
listvalue.add(hash2);// 把hashmap 加入List集合中
}
}
return listvalue;
}
public static List<HashMap<String, String>> loadString(String path) throws Exception{
//HttpGet 方法访问网络,不能携带参数,除了不鞥携带参数其他的和HttpPost 方法一样的!!!
HttpClient client = new DefaultHttpClient();HttpGet get = new HttpGet(path);
HashMap<String, String> hash ;
List<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
HttpResponse response = client.execute(get);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String jsonarray = EntityUtils.toString(entity);
JSONArray array = new JSONArray(jsonarray);
for(int i=0;i<array.length();i++){
JSONObject object = array.getJSONObject(i);
Iterator<String> it = object.keys();
hash = new HashMap<String, String>();
while(it.hasNext()){
String name = it.next();
String value = object.getString(name);
hash.put(name, value);
}
list.add(hash);
}
}
return list;
}