2010-07-17 20 views
3

我想從Android上的相機捕獲圖像,並將其發送到Google App Engine,該應用程序將圖像存儲在blob存儲區中。聽起來很簡單,我可以獲得多部分POST到GAE,但存儲到Blob存儲需要servlet返回一個HTTP重定向(302)。所以,我需要一個可以在執行HTTP POST之後遵循重定向的連接。這裏是我的代碼WISH將工作:最佳方法:從Android到GAE的HTTP POST(多部分)

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那樣的庫,但它需要很多的依賴關係,這對於一個簡單的任務來說似乎過於膨脹。

謝謝

+0

你確實需要關注重定向嗎?如果您只需要知道圖像上傳成功,您只需檢查是否返回了正確的重定向。 – 2010-07-19 11:14:45

+0

是的,我需要遵循重定向。你看,當圖像上傳到Blobstore時,會生成一個ID。爲了引用圖像,您需要在某處記錄該ID,這是由於重定向而在Servlet中發生的。 – 2010-07-19 18:21:55

回答

4

幾個月前,我試圖做同樣的事情!基本上不要使用HttpURLConnection。相反,在作爲Android SDK的一部分的org.apache.http包中使用HttpClientHttpGet

不幸的是,我沒有我的源代碼來提供一個例子,但希望這會讓你朝着正確的方向發展。

+0

是的,我相信這是一個解決的問題...你的方法是我採取的方法,增加了'MultipartEntity'。不過,我不知道爲什麼上面的代碼不起作用。 – 2010-07-18 02:45:49

0

不知道哪裏出了問題,但你可以按照自己的重定向。將位置標題取出並建立新的連接。