开发者

How do I add name value data to an android HTTP file upload POST request

I have an android photo upload script that uploads fine if I use $_GET to read some data, I want to use all $_POST. The problem is I need to append data do the posted request to include basic name value pairs along with the file upload. I can get this to work if I have the server use $_GET and just add the extra data in the url string, but I want to use post. Is there a way to encode other post data in the file upload that the server can read using $_POST. Heres the function, notice the URL string where, I'm trying to get the variables api_key,session_key, and method to be sent in the POST request.

public void uploadPhoto(FileInputStream fileInputStream, String sessionKey) throws IOException, ClientProtocolException {

    URL connectURL = new URL(API_URL +"?api_key=" +API_KEY+ "&session_key=" + sessionKey + "&method=" + ApiMethods.UPLOAD_PHOTO); //server ignores this data

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

    HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();

    // Allow Inputs
    conn.setDoInput(true);

    // Allow Outputs
    conn.setDoOutput(true);

    // Don't use a cached copy.
    conn.setUseCaches(false);

    // Use a post method.
    conn.setRequestMethod("POST");

    conn.setRequestProperty("Connection", "Keep-Alive");

    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ boundary);

    DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

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


    // create a buffer of maximum size

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

    // read file and write it into form...

    int 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);
        /开发者_如何学编程*
         * dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary +
         * twoHyphens + lineEnd);
         */
    }

    // send multipart form data necesssary after file data...
    dos.writeBytes(lineEnd);
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    dos.flush();
    // Log.d(TAG, "  dos5: " + dos.toString());
    InputStream is = conn.getInputStream();
    // retrieve the response from server
    int ch;

    StringBuffer b = new StringBuffer();
    while ((ch = is.read()) != -1) {
        b.append((char) ch);
    }
    String s = b.toString();
    dos.close();

}

EDIT I'm thinking something like the following is needed for each added param

    dos.writeBytes("--" + boundary + lineEnd);
    dos.writeBytes("Content-Disposition: form-data;name=api_key=" + lineEnd + lineEnd + API_KEY);
    dos.writeBytes(lineEnd);
    dos.writeBytes("--" + boundary + "--" + lineEnd);


There are some existing open libraries that support mime encoding which might save you some time. I used the code from here in some stuff I hacked up for posting:

http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/

You can get the apache-mime4j and httpmime libraries from here:

http://code.google.com/p/osmdroid/source/browse/trunk/osmdroid-google-wrapper/lib/?r=769

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜