【问题标题】:Android to Php server communicaton using HttpURLConnectionAndroid 使用 HttpURLConnection 与 PHP 服务器通信
【发布时间】:2014-10-29 12:32:28
【问题描述】:

我的项目是上传带有一些参数(如描述和日期)的图像、音频文件。

虽然 Google 宣布使用 HttpURLConnection 代替 httpclient。我正在使用 HttpURLConnection。

我有一个将图像和音频上传到服务器文件夹的代码。

但是服务器没有收到我发送的描述。

在 Stackover 流程​​中很多人都喜欢这个问题。但我没有得到确切的解决方案。

我的安卓代码是:

                FileInputStream fileInputStream = new FileInputStream(sourceFile_image);
                URL url = new URL(upLoadServerUri);
                conn = (HttpURLConnection) url.openConnection();
                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);

                //adding parameter 

                String description = ""+"Desceiption about the image";

                // Send parameter #name
                dos.writeBytes("Content-Disposition: form-data; name=\"description\"" + lineEnd);
                dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
                dos.writeBytes("Content-Length: " + description.length() + lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(description + lineEnd);
                dos.writeBytes(twoHyphens + boundary + lineEnd);

                // Send #image

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

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

                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);

和 PHP 代码:

$description= $_POST['description'];

   $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
   if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
       echo "success";
   } else{
       echo "fail";
   }

图像和音频更新成功。

但是没有收到参数或者我不知道如何在php中接收参数。

我的android和php代码发送和接收参数是否正确?

还有其他解决方案吗。

我尝试了很多,但没有工作,也没有得到想法。

【问题讨论】:

  • 您可以尝试使用 curl 来发帖以确保 php 部分正常工作

标签: java php android httpurlconnection multipartform-data


【解决方案1】:

检查这个link

安卓代码:

String description = ""+"Desceiption about the image";


            dos.writeBytes("Content-Disposition: form-data; name=\"description\"" + lineEnd); 
            //dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
            //dos.writeBytes("Content-Length: " + description.length() + lineEnd);
            dos.writeBytes(lineEnd);
            dos.writeBytes(description); // mobile_no is String variable
            dos.writeBytes(lineEnd);

php代码:

$description =$_POST['description'];

【讨论】:

【解决方案2】:
// Send parameter #name
dos.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd);

应该是:

// Send parameter #description
dos.writeBytes("Content-Disposition: form-data; name=\"description\"" + lineEnd);

【讨论】:

  • 你修改了但没有工作先生。android代码中添加的参数是否正确?图像和音频存储 ?但无法获取参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
相关资源
最近更新 更多