今天長沙中公優(yōu)就業(yè)java培訓機構小編為大家分享“三種實現(xiàn)java調用接口的方法”,希望通過此文能夠幫助到那些正在學習java的零基礎學員,下面就隨小編一起看看三種實現(xiàn)java調用接口的方法。
1、get
public String doGet(String url) {? ? try {? ? ? ? URL thisurl = new URL(url); // 把字符串轉換為URL請求地址? ? ? ? HttpURLConnection connection = (HttpURLConnection) thisurl? ? ? ? ? ? ? ? .openConnection();// 打開連接? ? ? ? connection.connect();// 連接會話? ? ? ? // 獲取輸入流? ? ? ? BufferedReader br = new BufferedReader(new InputStreamReader(? ? ? ? ? ? ? ? connection.getInputStream(), "UTF-8"));? ? ? ? String line;? ? ? ? StringBuilder sb = new StringBuilder();? ? ? ? while ((line = br.readLine()) != null) {// 循環(huán)讀取流? ? ? ? ? ? sb.append(line);? ? ? ? }? ? ? ? br.close();// 關閉流? ? ? ? connection.disconnect();// 斷開連接? ? ? ? return sb.toString();? ? } catch (Exception e) {? ? ? ? e.printStackTrace();? ? ? ? // System.out.println("失敗!");? ? ? ? return null;? ? }}
使用jdk自帶的java.net.URL;即可
2、delete
public String doDelete(String url) {? ? try {? ? ? ? URL thisurl = new URL(url);? ? ? ? HttpURLConnection connection = (HttpURLConnection) thisurl? ? ? ? ? ? ? ? .openConnection();? ? ? ? connection.setRequestMethod("DELETE");//就是這里不一樣? ? ? ? connection.connect();? ? ? ? // 獲取輸入流? ? ? ? BufferedReader br = new BufferedReader(new InputStreamReader(? ? ? ? ? ? ? ? connection.getInputStream(), "UTF-8"));? ? ? ? String line;? ? ? ? StringBuilder sb = new StringBuilder();? ? ? ? while ((line = br.readLine()) != null) {// 循環(huán)讀取流? ? ? ? ? ? sb.append(line);? ? ? ? }? ? ? ? br.close();// 關閉流? ? ? ? connection.disconnect();? ? ? ? return sb.toString();? ? } catch (Exception e) {? ? ? ? e.printStackTrace();? ? ? ? // System.out.println("失敗!");? ? ? ? return null;? ? }}
使用jdk自帶的java.net.URL;即可
3、post
private void recieveMsg(String url, String isliID) {? ? ? ? ? ? ? ? ? ? ? ? HttpClient httpClient = new HttpClient();? ? ? ? ? ? PostMethod postMethod = new PostMethod(url);? ? ? ? ? ? // 填入各個表單域的值,修改這個即可? ? ? ? ? ? NamevaluePair[] data = { new NamevaluePair("q", isliID) };? ? ? ? ? ? // 將表單的值放入postMethod中? ? ? ? ? ? postMethod.setRequestBody(data);? ? ? ? ? ? int statusCode = 0;? ? ? ? ? ? statusCode = httpClient.executeMethod(postMethod);? ? ? ? ? ? if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY? ? ? ? ? ? ? ? ? ? || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {? ? ? ? ? ? ? ? // 從頭中取出轉向的地址? ? ? ? ? ? ? ? Header locationHeader = postMethod? ? ? ? ? ? ? ? ? ? ? ? .getResponseHeader("location");? ? ? ? ? ? ? ? String location = null;? ? ? ? ? ? ? ? if (locationHeader != null) {? ? ? ? ? ? ? ? ? ? location = locationHeader.getValue();? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? return null;? ? ? ? ? ? } else {? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //調用成功? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println(postMethod.getStatusLine());? ? ? ? ? ? ? ? String str = "";? ? ? ? ? ? ? ? try {? ? ? ? ? ? ? ? ? ? str = postMethod.getResponseBodyAsString();? ? ? ? ? ? ? ? ? ? BufferedReader reader = new BufferedReader(new InputStreamReader(? ? ? ? ? ? ? ? ? ? ? ? ? ? postMethod.getResponseBodyAsStream(), "UTF-8"));? ? ? ? ? ? ? ? ? ? ?StringBuilder sb = new StringBuilder();? ? ? ? ? ? ? ? ? ? ?String line = null;? ? ? ? ? ? ? ? ? ? ?while ((line = reader.readLine()) != null) {? ? ? ? ? ? ? ? ? ? ?sb.append(line);? ? ? ? ? ? ? ? ? ? ?System.out.println(line);? ? ? ? ? ? ? ? ? ? ?}? ? ? ? ? ? ? ? } catch (IOException e) {? ? ? ? ? ? ? ? ? ? // TODO Auto-generated catch block? ? ? ? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? System.out.println(str);? ? ? ? ? ? }? ? ? ? ? ? postMethod.releaseConnection();}
使用的jar是
import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NamevaluePair; import org.apache.commons.httpclient.methods.PostMethod;
以上就是長沙中公優(yōu)就業(yè)java培訓機構小編分享的“三種實現(xiàn)java調用接口的方法”的內容,希望對大家有幫助,更多java最新資訊請繼續(xù)關注長沙中公優(yōu)就業(yè)java培訓機構官網,每天會有精彩內容分與你。
相關免費視頻教程推薦——java菜鳥教程下載:
java接口使項目分層代碼實現(xiàn):http://www.bjpowernode.com/xiazai/2567.html
java接口使項目分層描述:http://www.bjpowernode.com/xiazai/2568.html
java面向接口編程:http://www.bjpowernode.com/xiazai/2569.html
java接口的應用:http://www.bjpowernode.com/xiazai/2570.html
java接口與抽象類的異同點:http://www.bjpowernode.com/xiazai/2571.html
java接口說明:http://www.bjpowernode.com/xiazai/2572.html
java接口的定義與實現(xiàn):http://www.bjpowernode.com/xiazai/2575.html