如何在HttpURLConnection的机器人添加参数 [英] How to add parameter in httpurlconnection android

查看:255
本文介绍了如何在HttpURLConnection的机器人添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code成功发送图像到服务器。

Below code send the images to server successfully.

不过,我需要的是必须添加喜欢的图像描述和图像日期为数据库中的一个参数。

But what I need is have to add an parameter like image description and image date to database.

我不知道如何在HttURLconnection添加参数。

I dont know how to add parameter in HttURLconnection.

    String fileName = sourceFileUri;

    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File sourceFile = new File(sourceFileUri);


            FileInputStream fileInputStream = new FileInputStream(sourceFile_imagepath);
            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);
            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);

            // Responses from the server (code and message)
            serverResponseCode = conn.getResponseCode();
            String serverResponseMessage = conn.getResponseMessage();

            Log.i("uploadFile", "HTTP Response is : "
                    + serverResponseMessage + ": " + serverResponseCode);

            if (serverResponseCode == 200) {

                runOnUiThread(new Runnable() {
                    public void run() {
                        String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
                                + " c:/wamp/www/echo/uploads";
                        messageText.setText(msg);
                        Toast.makeText(MainActivity.this,
                                "File Upload Complete.", Toast.LENGTH_SHORT)
                                .show();
                    }
                });
            }

            // close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();

和PHP code:

<?php
  error_reporting(0);
   $file_path = "uploads/";

   $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";
   }
?>

在code以上运作良好上传code到服务器。

The code above is working well for uploading code to server.

要存储在数据库图像描述。

Have to store image description in db.

如何参数添加到上面的code。

How to add parameter to above code.

和如何接受它在PHP服务器端。

And how to receive it in php server side.

任何相关的教程或文档。

Any related tutorial or documentation.

在此先感谢。

推荐答案

勾选此<一个href="http://stackoverflow.com/questions/26686806/httpurlconnection-to-send-image-audio-and-video-files-with-parameter-may-stri">link

Android的code:

Android code:

字符串参数=值;

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

PHP的code:

Php code:

$parameter =$_POST['parameter'];

这篇关于如何在HttpURLConnection的机器人添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆