服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - httpclient 请求http数据,json转map的实例

httpclient 请求http数据,json转map的实例

2020-07-21 11:28jingxian Java教程

下面小编就为大家带来一篇httpclient 请求http数据,json转map的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

实例如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.idc.dataAudit.dataResource;
 
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
 
 
public class RequestHttpManage {
    public Map<Integer,String> RequesthttpInfo(String url) {
        String resultStr = null;
        Map<Integer,String> mapResult = new HashMap<Integer,String>();
    try{
            HttpClient client = new HttpClient(); 
            HttpMethod method=new GetMethod(url);
            //使用POST方法
            //HttpMethod method = new PostMethod("http://java.sun.com");
            client.executeMethod(method);
            
            //打印服务器返回的状态
            //System.out.println(method.getProtocolVersion());
            //System.out.println(method.getStatusLine().getStatusCode());
            //System.out.println(method.getStatusLine().getReasonPhrase());
              // System.out.println(method.getStatusLine().toString());
              // System.out.println(method.getStatusLine());
            //打印返回的信息
              // System.out.println(method.getResponseBodyAsString());
            
            
            mapResult.put(method.getStatusLine().getStatusCode(), method.getResponseBodyAsString());
            //释放连接
            method.releaseConnection();
    }catch(Exception e){
        e.printStackTrace();
    }
    System.out.println(mapResult);
    return mapResult;
  }
    public Map<String,List<String>> GetMapFromJson(String jsonStr){
    Map<String,List<String>> mapResult = new HashMap<String,List<String>>();
    try{
        
         JSONObject result = JSONObject.fromObject(jsonStr);//转换为JSONObject
      
       JSONArray nameList = result.getJSONArray("result");//获取JSONArray
       int length = nameList.size();
       for(int i=0;i<length;i++){
         JSONObject tempObject = nameList.getJSONObject(i);
         
         Iterator iter = tempObject.keys();
         String key = (String)iter.next();
         
         
         String keyId = tempObject .getString(key);
         
         List<String> objList = new ArrayList<String>();
         for (; iter.hasNext();) {
            String keyLocal = (String)iter.next();
          //  System.out.println(key+"="+tempObject .getString(keyLocal));
            objList.add(tempObject .getString(keyLocal));
         }
         mapResult.put(keyId, objList);
       }
    }catch(Exception e){
        e.printStackTrace();
    }
    System.out.println(mapResult);
    return mapResult;
  }
 
 
 
}

以上就是小编为大家带来的httpclient 请求http数据,json转map的实例全部内容了,希望大家多多支持服务器之家~

延伸 · 阅读

精彩推荐