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.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法