java 怎么获取url接口json数据
1个回答
展开全部
String oParams = "Field1=10" ; //设置参数
String url = "/api/SimpleInfoDocumentByField";
HttpClient httpclient = new HttpClient();
httpclient.getHostConfiguration().setHost("192.168.0.231", 7000, "http");
String urlBody = null;
try {
GetMethod getMethod= new GetMethod(url + "?" + oParams);
getMethod.setRequestHeader("Content-type", "text/JSON; charset=UTF-8");
// 链接超时(单位毫秒)
httpclient.getHttpConnectionManager().getParams()
.setConnectionTimeout(3000);
// 读取超时(单位毫秒)
httpclient.getHttpConnectionManager().getParams()
.setSoTimeout(3000);
int statusCode = httpclient.executeMethod(getMethod);
if (statusCode == 200) {
urlBody = getMethod.getResponseBodyAsString();
}
getMethod.releaseConnection();
} catch(Exception e){
e.printStackTrace();
}
还有一种postMethod,自己百度吧。
String url = "/api/SimpleInfoDocumentByField";
HttpClient httpclient = new HttpClient();
httpclient.getHostConfiguration().setHost("192.168.0.231", 7000, "http");
String urlBody = null;
try {
GetMethod getMethod= new GetMethod(url + "?" + oParams);
getMethod.setRequestHeader("Content-type", "text/JSON; charset=UTF-8");
// 链接超时(单位毫秒)
httpclient.getHttpConnectionManager().getParams()
.setConnectionTimeout(3000);
// 读取超时(单位毫秒)
httpclient.getHttpConnectionManager().getParams()
.setSoTimeout(3000);
int statusCode = httpclient.executeMethod(getMethod);
if (statusCode == 200) {
urlBody = getMethod.getResponseBodyAsString();
}
getMethod.releaseConnection();
} catch(Exception e){
e.printStackTrace();
}
还有一种postMethod,自己百度吧。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询