本文目錄一覽:
如何用Java調用別人API介面
java發一個http請求過去,帶上參數就可以了啊,跟我們在瀏覽器上訪問資源是一樣的 只是它返回的是json格式的數據而已
給你兩個方法吧:
public static String do_post(String url, ListNameValuePair 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;
}
怎麼使用java調用api介面
java發一個http請求過去,帶上參數就可以了啊,跟我們在瀏覽器上訪問資源是一樣的 只是它返回的是json格式的數據而已給你兩個方法吧:public static String do_post(String url, List name_value_pair) throws IOException { String body = “{}”
如何在java中調用api介面
需要導入對應的lib包,然後調用具體的介面以及方法
通過實現http協議進行post或者get請求具體api介面
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/248631.html