java通过http请求拿数据

  我们通常做Java后台接口,是让前端访问的,让前端获取数据或者做增删改查,但是有时候,我们做的Java接口是用来接收其他语言程序(如:python)查询到的结果,Java当中间件将其他http请求查询到的json结果处理后传给前端,本次做的处理只是记录每次查询时间,结果,查询条件,没有查到结果的请求给出友好提示,同时记录.

propreties文件中添加
abnormal.view =http\://000.000.000.00\:8080/abnormal_view
package **.***.view.web;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springside.modules.mapper.JsonMapper;

import **.***.view.entity.Log;
import **.***.view.entity.Params;
import **.***.view.service.LogService;

import com.google.common.collect.Maps;

@RestController
@RequestMapping(value="json")
public class DataController {
	private final JsonMapper mapper = JsonMapper.nonDefaultMapper();

	@Value("${abnormal.view}")
	private String url;


        private String getUrl() {
		return url;
	}

	
	@Autowired 
	private LogService logService;
	
	@RequestMapping(value="data",method = RequestMethod.POST)
	public String Json(@RequestBody Params params){
		Log data = this.getJson(params);
		Map<String,Object> result = Maps.newHashMap();
		result.put("data", data.getJson());
		//查不到数据时,python返回"{}\n"
		if(data.getJson().length()<=3){
			result.put("description","系统没有查询到数据,请修改查询条件");
			result.put("status", "blank");
		}else{
			result.put("description","数据请求成功");
			result.put("status", "ok");
		}
		return mapper.toJson(result);
	}

    	public Log getJson(Params params) {
		String requestJson = mapper.toJson(params);
		Log log = new Log();
		long startTimeMillis = System.currentTimeMillis();
		String responseJson = HttpUtils.doPostRequest(requestJson, this.getUrl());
		long endTimeMillis = System.currentTimeMillis();
		log.setParams(requestJson);
		log.setJson(responseJson);
		log.setDate(getCurrentTime());
		log.setRequestTime(String.valueOf(endTimeMillis-startTimeMillis));
		//查不到数据时,python返回"{}\n"
		if(responseJson.length()<=3){
			log.setStatus("blank");
		}else{
			log.setStatus("ok");
		}
		logDao.save(log);
		return log;
	}


}
package **.***.view.http;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


public class HttpUtils {

	
	@SuppressWarnings({ "resource", "deprecation" })
    //requestJson:请求参数,url:调用properties文件中的http请求
	public static String doPostRequest(String requestJson, String url) {
		HttpClient httpClient = null;
		HttpPost httpPost = null;
		String result = null;
		Map<String, String> params = new HashMap<String, String>();
		httpClient = new DefaultHttpClient();
		httpPost = new HttpPost(url);
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		
		//params.put("patient_id", requestJson);
		params.put("data", requestJson);
		Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
		while (iterator.hasNext()) {
			Entry<String, String> elem = (Entry<String, String>) iterator.next();
			list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
		}

		UrlEncodedFormEntity entity = null;

		try {
			entity = new UrlEncodedFormEntity(list, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		httpPost.setEntity(entity);
		HttpResponse response = null;
		try {
			response = httpClient.execute(httpPost);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (response != null) {
			HttpEntity resEntity = response.getEntity();
			try {
				result = EntityUtils.toString(resEntity, "utf-8");
			} catch (ParseException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值