码迷,mamicode.com
首页 > Web开发 > 详细

HttpRequest(HttpClient)

时间:2016-04-27 20:50:40      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

    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);

    }

 

HttpRequest(HttpClient)

标签:

原文地址:http://www.cnblogs.com/g-sheng/p/5440029.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!