开发者

Best approach: HTTP POST (multi-part) from Android to GAE

I would like to capture an image from the camera on Android, and send it to Google App Engine, which will store the image in the blob store. Sounds simple enough, and I can get the multi-part POST to GAE happening, but storing to the Blob store requires the servlet return an HTTP redirect (302). So, I need a connection that can follow redirects after doing an HTTP POST. Here is the code I WISH would work:

public static String sendPhoto(String myUrl, byte[] imageData) {
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    String pathToOurFile = "/data/file_to_send.jpg";
    String urlServer = myUrl;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    // Please follow redirects
    HttpURLConnection.setFollowRedirects(true);
    BufferedReader rd = null;
    StringBuilder sb = null;
    String line = null;
    try {
        URL url = new URL(urlServer);
        connection = (HttpURLConnection) url.openConnection();
        // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        // I really want you to follow redirects
        connection.setInstanceFollowRedirects(true);
        connection.setConnectTimeout(10000); // 10 sec
        connection.setReadTimeout(10000); // 10 sec
        // Enable POST method
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type",
            "multipart/form-data;boundary=" + boundary);
        connection.connect();
        outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream
                .writeBytes("Content-Disposition: form-data; name="
                    + "\"file1\";filename=\""
                    + pathToOurFile + "\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.write(imageData);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens
            + lineEnd);
        outputStream.flush();
        outputStream.close();
        rd = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        sb = new StringBuilder();
        while ((line = rd.readLine()) != null) {
            sb.append(line + '\n');
        }
        Log.i("Response: ", sb.toString());
        // Responses from the server (code and message)
        int serverResponseCode = connection.getResponseCode();
        String serverResponseMessage = connection.getResponseMessage();
        Log.i("Response: serverResponseCode:",
            String.valueOf(serverResponseCode));
        Log.i("Response: serverResponseMessage:", serverResponseMessage);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

As much as I try, this code will not follow redirects. The input steam is always em开发者_运维问答pty, and the response is always 302. The image is uploaded nicely though. If someone could tell me what I can do to make it follow the redirect so I can read the response, I would really appreciate it.

Alternatively, if there is a better approach, I would love to hear it. I know there are libraries out there like Apache HTTP Client, but it requires so many dependencies that is seems like too much bloat for a simple task.

Thanks


A few months ago I was trying to do the exact same thing!

Basically, don't use HttpURLConnection. Instead, use HttpClient and HttpGet in the org.apache.http package which are part of the Android SDK.

Unfortunately I don't have my source code to provide an example, but hopefully that'll set you in the right direction.


not sure what went wrong, but you can follow redirect yourself. take the Location header out and make a new connection to it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜