你是在找這個嗎?如何撥打java/Android
撥打網絡服務?如果你正在尋找此代碼,然後探索這個=>How to Get Web-Service Call using java Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class NetClientPost {
// http://localhost:8080/RESTfulExample/json/product/post
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/RESTfulExample/json/product/post");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = "{\"qty\":100,\"name\":\"iPad 4\"}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
請使用'postman'一個更好的客戶端調用'webservices' –
@VikrantKashyap感謝您的答覆,但不是郵差只是用於測試的API ?我想實際上從**我的應用發送一個GET請求**。郵遞員可以幫我嗎?如果有,你能告訴我更多嗎?謝謝。 –
郵遞員用來檢查你的web服務是否正常工作?它只是一個調用Web服務的工具,而不是來自任何Android應用@Rui Zhe –