java调用远程接口并接收返回json串

13 篇文章 0 订阅
10 篇文章 0 订阅

工具类:

package cn.com.dzqc.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class HttpClientUtil {
	 public static String requestByGetMethod(String s) {
	        CloseableHttpClient httpClient = HttpClients.createDefault();
	        StringBuilder entityStringBuilder = null;
	        try {
	            HttpGet get = new HttpGet(s);
	            CloseableHttpResponse httpResponse = null;
	            httpResponse = httpClient.execute(get);
	            try {
	                HttpEntity entity = httpResponse.getEntity();
	                entityStringBuilder = new StringBuilder();
	                if (null != entity) {
	                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"), 8 * 1024);
	                    String line = null;
	                    while ((line = bufferedReader.readLine()) != null) {
	                        entityStringBuilder.append(line + "/n");
	                    }
	                }
	            } finally {
	                httpResponse.close();
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                if (httpClient != null) {
	                    httpClient.close();
	                }
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	        return entityStringBuilder.toString();
	    }

}

 Controller:

@RequestMapping(value="/getjson")
	@ResponseBody
	public String getjson(String aa){
	    String url = "http://127.0.0.1/aaa/aaa/aaa/selectaa"+"?aa="+aa;
            String res = null;
		try {
			  res = HttpClientUtil.requestByGetMethod(url);
	            JSONObject result = new JSONObject();
	            System.out.println(res);
	            res = res.substring(res.indexOf("{"), res.indexOf("}") + 1);
	            System.out.println(res);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			
		}
		return res;
	}

 这样就可以了

前端ajax

                        $.ajax({
    				url:"${path}/cost/getjson",
    				type:"post",
    				data: {
    					aa: aa
    				},
    				dataType:'json',  //一定要加上这个,告诉他返回json数据
    				success:function(res){
						console.log("----")
						layer.closeAll()
						if(res.code == 200){ 
							window.location.href = "www.baidu.com"
						} else {
							console.log(res.msg)
							  layer.open({
							    content: res.msg
							    ,btn: '确定'
							  });
						}
    				}
    			})

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值