zoukankan      html  css  js  c++  java
  • HttpClient调用http接口(POST)

    调用方法,传过去接口地址和参数(json对象或者map,在方法里转成json字符串)

    JSONObject.toJSONString(map)

    JSONObj.toString()

    public static String getResult(String url,Map<String, String> params){
            String returnValue = null;
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 15000);
            try {
                 HttpPost httppost = new HttpPost(url);
                 httppost.addHeader("Content-type","application/json; charset=utf-8");  
                 httppost.setHeader("Accept", "application/json");
                
                 StringEntity entity = new StringEntity(JSONObject.toJSONString(params),Charset.forName("UTF-8"));    
                 httppost.setEntity(entity);
                 
                 HttpResponse resp = httpclient.execute(httppost);
                 if(resp.getStatusLine().getStatusCode() == 200) {
                    HttpEntity he = resp.getEntity();
                    String   respContent = EntityUtils.toString(he,"UTF-8");
                    returnValue =  respContent;
                 }
            }
            catch (SocketTimeoutException e) {
                 e.printStackTrace();
                 returnValue = "timeout";
            }catch (Exception e) {
                e.printStackTrace();
            }finally {
               // 关闭连接,释放资源
                httpclient.getConnectionManager().shutdown();
           }
           return  returnValue;
        }

  • 相关阅读:
    iOS:UIToolBar、toolbarItems、BarButtonItem的几种关系
    iOS:UIToolBar控件的使用
    iOS:制作九宫格
    iOS:转载:UIControl的使用
    iOS:UIPickerView选择器的使用
    iOS:NSDate的主要几种时间形式
    iOS:步进UIStepper、滑动块UISlider、开关UISwitch的基本使用
    iOS:NSBundle的具体介绍
    h264码流分析及其工具
    零基础学习视频解码之FFMpeg中比较重要的函数以及数据结构
  • 原文地址:https://www.cnblogs.com/zhuangwf/p/11398708.html
Copyright © 2011-2022 走看看