最佳方法:从Android到GAE的HTTP POST(多部分)

时间:2010-07-17 05:08:28

标签: java android http google-app-engine multipart

我想从Android上的相机中捕获图像,并将其发送到Google App Engine,后者会将图像存储在blob存储中。听起来很简单,我可以将多部分POST发送到GAE,但是存储到Blob存储需要servlet返回HTTP重定向(302)。因此,我需要一个可以在执行HTTP POST后遵循重定向的连接。这是我 WISH 的代码:

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

尽我所能,此代码不会遵循重定向。输入蒸汽始终为空,响应始终为302.虽然图像上传得很好。如果有人能告诉我我可以做些什么来使其遵循重定向,那么我可以阅读回复,我真的很感激。

或者,如果有更好的方法,我很乐意听到它。我知道有像Apache HTTP Client那样的库,但它需要这么多的依赖项,对于一个简单的任务来说似乎太过臃肿。

由于

2 个答案:

答案 0 :(得分:4)

几个月前,我试图做同样的事情!

基本上,请勿使用HttpURLConnection。相反,请在HttpClient包中使用HttpGetorg.apache.http,它们是Android SDK的一部分。

不幸的是,我没有提供示例的源代码,但希望这会让你朝着正确的方向前进。

答案 1 :(得分:0)

不确定出了什么问题,但您可以自己重定向。取出Location标头并建立新的连接。