douwen5951 2010-11-23 07:15
浏览 69

如何在android HttpURLConnection中模仿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!!)

  • 写回答

1条回答 默认 最新

  • dougu3988 2010-11-23 08:27
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊