测试HttpClient访问接口并在控制台输出结果的样例

    xiaoxiao2022-06-24  41

    import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class TestGet { public static void main(String[] args) { new Get().start(); } } class Get extends Thread{ HttpClient client = HttpClients.createDefault(); @Override public void run(){ HttpGet get1 = new HttpGet("http://zspay.zhongsou.com/Useraction/getZsbLog?user=15179024714&start=2016-08-01 00:00:00&end=2016-08-07 00:00:00&sign=3BC68DFE84F9B8DFFC5BDEB0A0932C33"); try{ HttpResponse response = client.execute(get1); HttpEntity entity = response.getEntity(); String result = decodeUnicode(EntityUtils.toString(entity)); JsonFormatTool tool = new JsonFormatTool();   System.out.println(tool.formatJson(result.toString())); }catch(ClientProtocolException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } HttpGet get2 = new HttpGet("http://zspay.zhongsou.com/Useraction/getZsbLog?user=15179024714&start=2016-08-08 00:00:00&end=2016-08-14 00:00:00&sign=3BC68DFE84F9B8DFFC5BDEB0A0932C33"); try{ HttpResponse response = client.execute(get2); HttpEntity entity = response.getEntity(); String result = decodeUnicode(EntityUtils.toString(entity)); JsonFormatTool tool = new JsonFormatTool();   System.out.println(tool.formatJson(result.toString())); }catch(ClientProtocolException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } HttpGet get3 = new HttpGet("http://zspay.zhongsou.com/Useraction/getZsbLog?user=15179024714&start=2016-08-15 00:00:00&end=2016-08-21 00:00:00&sign=3BC68DFE84F9B8DFFC5BDEB0A0932C33"); try{ HttpResponse response = client.execute(get3); HttpEntity entity = response.getEntity(); String result = decodeUnicode(EntityUtils.toString(entity)); JsonFormatTool tool = new JsonFormatTool();   System.out.println(tool.formatJson(result.toString())); }catch(ClientProtocolException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } HttpGet get4 = new HttpGet("http://zspay.zhongsou.com/Useraction/getZsbLog?user=15179024714&start=2016-08-22 00:00:00&end=2016-08-28 00:00:00&sign=3BC68DFE84F9B8DFFC5BDEB0A0932C33"); try{ HttpResponse response = client.execute(get4); HttpEntity entity = response.getEntity(); String result = decodeUnicode(EntityUtils.toString(entity)); JsonFormatTool tool = new JsonFormatTool();   System.out.println(tool.formatJson(result.toString())); }catch(ClientProtocolException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } HttpGet get5 = new HttpGet("http://zspay.zhongsou.com/Useraction/getZsbLog?user=15179024714&start=2016-08-29 00:00:00&end=2016-08-31 00:00:00&sign=3BC68DFE84F9B8DFFC5BDEB0A0932C33"); try{ HttpResponse response = client.execute(get5); HttpEntity entity = response.getEntity(); String result = decodeUnicode(EntityUtils.toString(entity)); JsonFormatTool tool = new JsonFormatTool();   System.out.println(tool.formatJson(result.toString())); }catch(ClientProtocolException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } public static String decodeUnicode(String theString) { char aChar; int len = theString.length(); StringBuffer outBuffer = new StringBuffer(len); for (int x = 0; x < len;) { aChar = theString.charAt(x++); if (aChar == '\\') { aChar = theString.charAt(x++); if (aChar == 'u') { // Read the xxxx int value = 0; for (int i = 0; i < 4; i++) { aChar = theString.charAt(x++); switch (aChar) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': value = (value << 4) + aChar - '0'; break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': value = (value << 4) + 10 + aChar - 'a'; break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': value = (value << 4) + 10 + aChar - 'A'; break; default: throw new IllegalArgumentException("Malformed   \\uxxxx   encoding."); } } outBuffer.append((char) value); } else { if (aChar == 't') aChar = '\t'; else if (aChar == 'r') aChar = '\r'; else if (aChar == 'n') aChar = '\n'; else if (aChar == 'f') aChar = '\f'; outBuffer.append(aChar); } } else outBuffer.append(aChar); } return outBuffer.toString(); } } class JsonFormatTool   {       /**       * 单位缩进字符串。       */       private static String SPACE = "   ";              /**       * 返回格式化JSON字符串。       *        * @param json 未格式化的JSON字符串。       * @return 格式化的JSON字符串。       */       public String formatJson(String json)       {           StringBuffer result = new StringBuffer();                      int length = json.length();           int number = 0;           char key = 0;                      //遍历输入字符串。           for (int i = 0; i < length; i++)           {               //1、获取当前字符。               key = json.charAt(i);                              //2、如果当前字符是前方括号、前花括号做如下处理:               if((key == '[') || (key == '{') )               {                   //(1)如果前面还有字符,并且字符为“:”,打印:换行和缩进字符字符串。                   if((i - 1 > 0) && (json.charAt(i - 1) == ':'))                   {                       result.append('\n');                       result.append(indent(number));                   }                                      //(2)打印:当前字符。                   result.append(key);                                      //(3)前方括号、前花括号,的后面必须换行。打印:换行。                   result.append('\n');                                      //(4)每出现一次前方括号、前花括号;缩进次数增加一次。打印:新行缩进。                   number++;                   result.append(indent(number));                                      //(5)进行下一次循环。                   continue;               }                              //3、如果当前字符是后方括号、后花括号做如下处理:               if((key == ']') || (key == '}') )               {                   //(1)后方括号、后花括号,的前面必须换行。打印:换行。                   result.append('\n');                                      //(2)每出现一次后方括号、后花括号;缩进次数减少一次。打印:缩进。                   number--;                   result.append(indent(number));                                      //(3)打印:当前字符。                   result.append(key);                                      //(4)如果当前字符后面还有字符,并且字符不为“,”,打印:换行。                   if(((i + 1) < length) && (json.charAt(i + 1) != ','))                   {                       result.append('\n');                   }                                      //(5)继续下一次循环。                   continue;               }                              //4、如果当前字符是逗号。逗号后面换行,并缩进,不改变缩进次数。               if((key == ','))               {                   result.append(key);                   result.append('\n');                   result.append(indent(number));                   continue;               }                              //5、打印:当前字符。               result.append(key);           }                      return result.toString();       }              /**       * 返回指定次数的缩进字符串。每一次缩进三个空格,即SPACE。       *        * @param number 缩进次数。       * @return 指定缩进次数的字符串。       */       private String indent(int number)       {           StringBuffer result = new StringBuffer();           for(int i = 0; i < number; i++)           {               result.append(SPACE);           }           return result.toString();       }   }  
    转载请注明原文地址: https://ju.6miu.com/read-1123997.html

    最新回复(0)