java中怎么调用api数据接口
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中怎么调用api数据接口相关的知识,希望对你有一定的参考价值。
java发一个http请求过去,带上参数就可以了啊,跟我们在浏览器上访问资源是一样的 只是它返回的是json格式的数据而已给你两个方法吧:
public static String do_post(String url, List<NameValuePair> name_value_pair) throws IOException
String body = "";
DefaultHttpClient httpclient = new DefaultHttpClient();
try
HttpPost httpost = new HttpPost(url);
httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
finally
httpclient.getConnectionManager().shutdown();
return body;
public static String do_get(String url) throws ClientProtocolException, IOException
String body = "";
DefaultHttpClient httpclient = new DefaultHttpClient();
try
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
finally
httpclient.getConnectionManager().shutdown();
return body;
参考技术A
java发一个http请求过去,带上参数就可以了啊,跟我们在浏览器上访问资源是一样的 只是它返回的是json格式的数据而已
给你两个方法吧:
String body = "";
DefaultHttpClient httpclient = new DefaultHttpClient();
try
HttpPost httpost = new HttpPost(url);
httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
finally
httpclient.getConnectionManager().shutdown();
return body;
public static String do_get(String url) throws ClientProtocolException, IOException
String body = "";
DefaultHttpClient httpclient = new DefaultHttpClient();
try
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
finally
httpclient.getConnectionManager().shutdown();
return body;
本回答被提问者和网友采纳
c#webapi怎么调用接口并传参数
参考技术A 使用WebApi也有段时间了,今天就记录下API接口传参的一些方式方法,算是一个笔记,也希望能帮初学者少走弯路以上是关于java中怎么调用api数据接口的主要内容,如果未能解决你的问题,请参考以下文章