我是新來的Android和現在我太累了搜索關於Android的HTTP請求的最佳教程。如何從Android發送http發佈請求?
我想這些參數發送到phpServer,
名稱= 「一些名」
電子郵件= 「一些電子郵件」
我們能做到這一點在MainActivity.java文件中?這是否需要任何jar(庫)文件? 歡迎任何建議。
我想這一點,
@Override
public void onClick(View view) {
if(view == btnSubscribe){
HttpURLConnection client = null;
try {
URL url = new URL("http://www/somewebsite.com/API/SUBSCRIBE");
client = (HttpURLConnection) url.openConnection();
client.setRequestMethod("POST");
client.setRequestProperty("name","test one");
client.setRequestProperty("email","[email protected]");
client.setDoOutput(true);
OutputStream outputPost = new BufferedOutputStream(client.getOutputStream());
outputPost.flush();
outputPost.close();
client.setChunkedStreamingMode(0);
} catch(MalformedURLException error) {
showError("Handles an incorrectly entered UR");
}
catch(SocketTimeoutException error) {
showError("Handles URL access timeout");
}
catch (IOException error) {
showError("Handles input and output errors");
}finally {
if(client != null)
client.disconnect();
}
}
}
其中代碼???? –
@Sahan通過去[這](http://stackoverflow.com/help/mcve) – Nisarg
@IntelliJAmiya對不起,我清除了所有的代碼,因爲我沒有工作。現在我正在一個新項目中。 – Sahan