1. 程式人生 > >java代碼調用第三方接口

java代碼調用第三方接口

ace return created pri stp exceptio close except charset

一、利用httpclient來字符串參數(url是第三方接口,不帶參數,如:http://192.168.16.200:8081/faceInfo/list,param是url後面所要帶的參數)

public static JSONObject getHttpResponseJson(String url,Map<String,String> param){
        CloseableHttpClient httpclient = null;
        CloseableHttpResponse response = null;
        JSONObject jsonObject 
= null; try { //請求發起客戶端 httpclient = HttpClients.createDefault(); //參數集合 List postParams = new ArrayList(); //遍歷參數並添加到集合 for(Map.Entry<String, String> entry:param.entrySet()){ postParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); }
//通過post方式訪問 HttpPost post = new HttpPost(url); HttpEntity paramEntity = new UrlEncodedFormEntity(postParams,"UTF-8"); post.setEntity(paramEntity); response = httpclient.execute(post); HttpEntity valueEntity = response.getEntity(); String content
= EntityUtils.toString(valueEntity); jsonObject = JSONObject.fromObject(content); return jsonObject; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(httpclient != null){ try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } if(response != null){ try { response.close(); } catch (IOException e) { e.printStackTrace(); } } } return jsonObject; }

二、利用httpclient來同時上傳文件和其他字符串參數(postUrl)

public static String getHttpResponseString(String postUrl,Map<String,String> filePathParam,Map<String,String> param){
        //1:創建一個httpclient對象
        HttpClient httpclient = new DefaultHttpClient();
        Charset charset = Charset.forName("UTF-8");//設置編碼
        try {
            //2:創建http的發送方式對象,是GET還是post
            HttpPost httppost = new HttpPost(postUrl);

            //3:創建要發送的實體,就是key-value的這種結構,借助於這個類,可以實現文件和參數同時上傳
            MultipartEntity reqEntity = new MultipartEntity();
            
            //遍歷圖片並添加到MultipartEntity實體中
            for(Map.Entry<String, String> entry:filePathParam.entrySet()){
                 FileBody fileContent = new FileBody(new File(entry.getValue()));
                 reqEntity.addPart(entry.getKey(),fileContent);
            }
            
            //遍歷參數並添加到MultipartEntity實體中
            for(Map.Entry<String, String> entry:param.entrySet()){
                StringBody content = new StringBody(entry.getValue(),charset);
                reqEntity.addPart(entry.getKey(), content);
            }
            
            httppost.setEntity(reqEntity);

            //4:執行httppost對象,從而獲得信息
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();

            //獲得返回來的信息,轉化為字符串string
            String resString = EntityUtils.toString(resEntity);
            return resString;

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
        }
        return null;
    }

java代碼調用第三方接口