【发布时间】:2016-11-19 08:57:05
【问题描述】:
我是 android 新手,现在我已经厌倦了寻找关于 android HTTP 请求的最佳教程。
我正在尝试将这些参数发送到 phpServer,
Name = "某个名字"
email = "一些电子邮件"
我们可以在 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","text@gmail.com");
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 通过this
-
@IntelliJAmiya 抱歉,我清除了所有代码,因为对我不起作用。现在我在一个新项目中。
-
@Nisarg 谢谢。
-
@IntelliJAmiya 伙计们现在可以看看吗?我试过了。
-
name应该是Name