使用android

时间:2018-06-29 16:31:57

标签: java php android post httpurlconnection

我正在尝试通过我的Android应用程序将两个图像以及一些参数上传到服务器。在网上搜索并遵循herehere以及其他来源的说明后,我得到了以下代码:

String boundary = "***" + System.currentTimeMillis() + "***";
String twoHyphens = "--";
String crlf = "\r\n";
String output = "";
try {
            HttpURLConnection httpUrlConnection = null;
            URL url = new URL(myUrl);
            httpUrlConnection = (HttpURLConnection) url.openConnection();
            httpUrlConnection.setUseCaches(false);
            httpUrlConnection.setDoInput(true);
            httpUrlConnection.setDoOutput(true);
            httpUrlConnection.setRequestMethod("POST");

            httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
            httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
            httpUrlConnection.setRequestProperty("ENCTYPE", "multipart/form-data");
            httpUrlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);


            DataOutputStream request = new DataOutputStream(httpUrlConnection.getOutputStream());
            request.writeBytes(twoHyphens + boundary + crlf);

            // Convert and add first image
            ByteArrayOutputStream bao1 = new ByteArrayOutputStream();
            params[0].compress(Bitmap.CompressFormat.JPEG, 100, bao1);
            byte[] ba1 = bao1.toByteArray();


            request.writeBytes("Content-Disposition: form-data; name=\"image1\";filename=\"image1\"" + crlf);
            request.writeBytes(crlf);
            request.write(ba1);
            request.writeBytes(crlf);
            request.writeBytes(twoHyphens + boundary + crlf);

            // Convert and add second image
            ByteArrayOutputStream bao2 = new ByteArrayOutputStream();
            params[1].compress(Bitmap.CompressFormat.JPEG, 100, bao2);
            byte[] ba2 = bao2.toByteArray();

            request.writeBytes("Content-Disposition: form-data; name=\"image2\";filename=\"image2\"" + crlf);
            request.writeBytes(crlf);
            request.write(ba2);
            request.writeBytes(crlf);
            request.writeBytes(twoHyphens + boundary + crlf);

            request.writeBytes("Content-Disposition: form-data; name=\"username\"" + crlf);
            request.writeBytes(crlf);
            request.writeBytes(username);
            request.writeBytes(crlf);
            request.writeBytes(twoHyphens + boundary + twoHyphens);

            request.writeBytes("Content-Disposition: form-data; name=\"datestr\"" + crlf);
            request.writeBytes(crlf);
            request.writeBytes(timeStampString);
            request.writeBytes(crlf);
            request.writeBytes(twoHyphens + boundary + twoHyphens);

            request.flush();
            request.close();

            int responseCode = httpUrlConnection.getResponseCode();
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                InputStream responseStream = new BufferedInputStream(httpUrlConnection.getInputStream());
                BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream, Charset.forName("UTF-8")));

                String line;
                while ((line = responseStreamReader.readLine()) != null) {
                    output = line;
                    Log.d(TAG, line);
                }
                responseStreamReader.close();
            }
            httpUrlConnection.disconnect();

            if (output == "") {
                httpResultsReturned = false;
            } else {
                httpResultsReturned = true;
            }

        } catch (ProtocolException e) {
            e.printStackTrace();
            return "failed";
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "failed";
        } catch (IOException e) {
            e.printStackTrace();
            return "failed";
        }

在服务器端,我尝试按以下方式访问数据:

<?php

if($_SERVER['REQUEST_METHOD'] === 'POST'){
    $image1 = $_FILES['image1']['name'];
    $image2 = $_FILES['image2']['name'];
    $datestr= $_POST['datestr'];
    $username= $_POST['username'];
}

?>

最终,两个图像都成功传输,但是我无法发送/接收额外的参数。我正确地收到了响应,但是在所有的php代码中(我在本问题中省略了某些部分),似乎没有参数被发送/接收。

this问题中,AndroSco共享了适用于他的解决方案,但是在他的php文件中,看来他仅访问图像而不是参数...

由于我在该领域没有太多经验,所以我认为可能很明显我做错了/根本没有做!

任何建议将不胜感激!

谢谢!

1 个答案:

答案 0 :(得分:0)

经过无奈之后,我在代码中找到了该错误。将两个图像导入传输的消息中之后,并且当我想导入其他参数时,我错误地编写了边界。而不是添加以下内容:

request.writeBytes(twoHyphens + boundary + crlf);

结尾处有新行,我这样写:

request.writeBytes(twoHyphens + boundary + twoHyphens);

在行尾添加两个连字符。

twoHyphens替换为crlf之后,一切正常!