zoukankan      html  css  js  c++  java
  • HttpRequest(HttpClient)

        public static String post(String interfaceType, JSONObject json) {
            String data = null;
            HttpClient client = new DefaultHttpClient();
            client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
            client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
            StringBuilder builder = new StringBuilder();
            String url_params = String.format(HttpConst.URL, interfaceType, jsonToParams(json));
            //HttpPost post = new HttpPost(url_params);
            HttpGet get=new HttpGet(url_params);
            try {
                HttpResponse response = client.execute(get);
                HttpEntity entity = response.getEntity();
                BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
                for (String s = reader.readLine(); s != null; s = reader.readLine()) {
                    builder.append(s);
                }
                data = JSONTokener(builder.toString());
            } catch (Exception e) {
                LogUtils.d(e);
            }
            return data;
        }
        
        public static String jsonToParams(JSONObject json) {
            String params = json.toString();
            params = params.replace("}", "");
            params = params.replace("{", "");
            params = params.replace(""", "");
            params = params.replace(":", "=");
            String[] strings = params.split(",");
            params = "";
            for (int i = 0; i < strings.length; i++) {
                params += strings[i] + "&";
            }
    
            return params.substring(0, params.length() - 1);
    
        }
  • 相关阅读:
    bzoj 1054
    bzoj 1047
    bzoj 2761
    bzoj 1191
    bzoj 2748
    bzoj_1003 物流运输
    新的开始( [USACO08OCT]打井Watering Hole)
    map
    Generic Cow Protests-G——60分做法
    逆序对
  • 原文地址:https://www.cnblogs.com/g-sheng/p/5440029.html
Copyright © 2011-2022 走看看