日期:2014-05-17  浏览次数:20731 次

关于HttpClient的问题,高手请进
这个怎么实现向服务器提交json参数呢?服务器返回的也是json参数

------解决方案--------------------
    public String getResponse(String urlStr) throws Exception{        HttpURLConnection connection = null;
        BufferedReader bufferedReader = null;
        try {
            // URL请求
              URL url = new URL(urlStr);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            // 请求结果取得
            bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Statics.CHARSET));
            String str = null;
            StringBuffer sb = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                sb.append(str);
            }
            
            return sb.toString();
        } catch (Exception e) {
            // 错误处理
            return "";
        } finally {
            if (bufferedReader != null) {
                bufferedReader.close();
                bufferedReader = null;
            }
            if (connection != null) {
                connection.disconnect();
                connection = null;
            }
            logger.debug("End");
        }
    }



这是一个传入参数为URL,将字符串返回的例子。我们项目中用的。

根据需求可自行将字符串解析为json等等

如果要传参,就挂在url后面。想用post传值,网上搜的用httpPost吧
------解决方案--------------------
http://zhcheng.iteye.com/blog/1292350

自己模仿下
------解决方案--------------------
http://fuliang.iteye.com/blog/