如何模仿“推送提交”的动作android HttpURLConnection 中 PHP 表单的按钮

发布于 2024-10-04 10:21:50 字数 1196 浏览 0 评论 0原文

我发现一些教程教我如何通过 PHP 和 html 表单将文件上传到我的服务器。

即使我在所有这些教程中总是失败:(...我仍然认为这些教程是正确的。

这些教程中的大多数都让我觉得我们可以在我们的服务器中用 PHP 制作元数据形式,并在 android 中使用 HttpURLConnection 类。

但是我无法弄清楚这些教程如何自动推送“提交”,就像我们通常在浏览器中所做的那样@@”

大多数教程都是这样的:(

  URL url =new URL(actionUrl);
  HttpURLConnection con=(HttpURLConnection)url.openConnection();
  con.setDoInput(true);
  con.setDoOutput(true);

  con.setUseCaches(false);
  con.setRequestMethod("POST");
  con.setRequestProperty("Connection", "Keep-Alive");
  con.setRequestProperty("Charset", "UTF-8");
  con.setRequestProperty("Content-Type",
                     "multipart/form-data;boundary="+boundary);
  //omit some code that start to output the file stream to url connection stream

  ds.writeBytes(end);
  ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
  //omit some code theat start read url connection response stream

很抱歉,我在这里省略了一些小代码行,但教程结果告诉我们在我们使用这些与 PHP 表单的连接设置之后,我们可以从另一个 PHP 操作页面获取响应,该页面在服务器中存储文件并返回有关该文件的一些信息)

所以,我想这些教程本质上是以某种方式按下“提交”按钮,因为我们从 PHP 操作页面而不是 PHP 表单(带有提交按钮)页面获得响应,

但我根本不知道他们是如何做到的......

任何人都可以帮助我吗

?有更好的解决方案将文件上传到服务器或使用PHP网页进行操作,也请给我一些建议!谢谢!!)

I've found some tutorial teaching me how to upload a file to my server through PHP and html form.

Even throught I always fail in all of these tutorial :(... I still assume these tutorial is correct.

Most of these tutorial tall me that we can make metatdata form in PHP in our server, and use HttpURLConnection class in android.

But I can't figure our how these tutorial automatically push the "submit" as what we usually do in browser @@"

Most these tutorial like this:

  URL url =new URL(actionUrl);
  HttpURLConnection con=(HttpURLConnection)url.openConnection();
  con.setDoInput(true);
  con.setDoOutput(true);

  con.setUseCaches(false);
  con.setRequestMethod("POST");
  con.setRequestProperty("Connection", "Keep-Alive");
  con.setRequestProperty("Charset", "UTF-8");
  con.setRequestProperty("Content-Type",
                     "multipart/form-data;boundary="+boundary);
  //omit some code that start to output the file stream to url connection stream

  ds.writeBytes(end);
  ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
  //omit some code theat start read url connection response stream

(I am sorry that i omit some lines of minor code here, but the tutorial result tell us that after we use these connection setup to PHP form, we can get the response from another PHP action page that stroe file in server and return some info about the file)

So, I guess these tutorial essentially push the "submit" button in someway, because we get response from action PHP page rather than the PHP form(with submit button) page.

But I can't figure out how they do it at all....

Anyone can help me??? Thx!!

(if you have better solution to upload file to server or manipulate with PHP webpage, please also give me some suggestion!! thx!!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半夏半凉 2024-10-11 10:21:50

HTML 中的Submit 按钮用于让用户有机会决定何时将表单数据发送到服务器。这里的代码实际上将数据发送到服务器:它打开一个 HTTP 连接,告诉服务器期望什么样的数据并发送数据(我认为 ds 是一个 数据输出流)。

因此,如果您希望 Android 应用程序中有一个按钮来触发这些操作,请将此代码放入按钮小部件的 OnClickListener 中。

A Submit button in HTML is used to give the user the chance to decide when to send the form data to the server. What you have here is code that actually sends data to the server: it opens an HTTP connection, it tells the server what kind of data to expect and sends the data (I suppose ds is a DataOutputStream).

So, if you want to have a button in your Android app which triggers these actions, put this code into an OnClickListener of a button widget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文