gpt4 book ai didi

java - android:将图像发布到服务器时如何添加edittext值

转载 作者:行者123 更新时间:2023-12-01 13:16:46 25 4
gpt4 key购买 nike

我是 Android 开发新手...
我发现这段代码用于将图像上传到服务器并且其工作完美......
只是我在布局中添加 edittext,并且我也希望将带有图像的 edittext 的值发布到服务器
我的部分代码:

 URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());

dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);

bytesAvailable = fileInputStream.available(); // create a buffer of maximum size

bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];

// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}

// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// Responses from the server (code and message)
int serverResponseCode = conn.getResponseCode();
//String serverResponseMessage = conn.getResponseMessage();
if(serverResponseCode == 200){
runOnUiThread(new Runnable() {
public void run() {

我的编辑文本:

<EditText
android:id="@+id/detail"
android:layout_width="match_parent"
android:layout_height="164dp"
android:layout_gravity="right"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:ems="10"
android:hint="About Your picture"
android:inputType="textMultiLine" >

任何人都可以用我的代码添加示例,我如何在此编辑文本中获取值并将其添加到表单
要发布此编辑文本的图像和值吗?

最佳答案

而不是这个

conn.setRequestProperty("firstName", "Stephen");

你必须使用

conn.setRequestProperty("firstName", urDetailEdtTxt.getText().toString());

对于这种post请求,如果keyfirstName,那么你需要一个Key-Value对否则它会起作用,将 key 替换为正确的 key 。

关于java - android:将图像发布到服务器时如何添加edittext值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22411048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com