2013-03-09 75 views
1

我在上GAE應用程式:http://1.myawesomecity.appspot.com/上傳文件到GAE從Android電子

FIXED:

   HttpPost post = new HttpPost("http://1.myawesomecity.appspot.com/"); 
       http_client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 
       String result = EntityUtils.toString(http_client.execute(post).getEntity(), "UTF-8"); 
       String actualURL = result.substring(result.indexOf("http://"), result.indexOf("\" method")); 
       Log.w("asdf", "url " + actualURL); 


       post = new HttpPost(actualURL); 
       http_client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);      
       MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
       String mime_type = "image/png"; 
       File file = new File(filename); //context.getFilesDir(), 
       entity.addPart("myFile", new FileBody(file, mime_type)); 
       post.setEntity(entity); 

       String res = EntityUtils.toString(http_client.execute(post).getEntity(), "UTF-8"); 
       Log.w("asdf", res); 

上面抓住從GAE服務器的實際載URL,並通過在該文件中正如下面的正確答案所指示的那樣。

老問題:

正如你所看到的,如果你選擇一個文件,並點擊提交,它將404,但該文件實際上沒有得到保存(只要不是太大,< 100KB)。不要在第一個文本字段中輸入任何內容。

現在,拋開這個特定的應用程序幾乎沒有功能,我試圖從Android上傳文件到這個服務器上。

該網站的上傳腳本使用blobstore,並且該文件字段的名稱是「myFile」。

現在在我的Android應用程序,我有:

HttpClient httpclient = new DefaultHttpClient(); 

    HttpPost httppost = new HttpPost(<my app's url>); 

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("myFile", <path to a file selected by user>)); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    httpclient.execute(httppost); 

這會拋出異常。

這與我通過瀏覽器進入我的網站,選擇文件並點擊提交有什麼不同?爲什麼通過瀏覽器實際上通過上傳文件,當Android代碼不?

我知道我的文件路徑是有效的。有什麼我做錯了嗎?或者是從不同於從Android執行httpclient的瀏覽器中點擊「提交」?

+0

它拋出什麼異常?請發佈堆棧跟蹤。 – wtsang02 2013-03-10 00:02:18

+0

請谷歌。知道爲什麼會發生NetworkOnMainThreadException,那麼你應該找到你的答案。 – wtsang02 2013-03-10 00:52:25

+0

我已經將它改回(AsyncThread)。它不會拋出異常。無論出於何種原因,我只能等待很長時間才能運行後臺進程。無論哪種方式,似乎我的文件沒有被上傳。 當通過瀏覽器爲文件填寫表格時,表格是否填寫爲本地計算機中文件的目錄?如果是這種情況,我不明白爲什麼我當前的asyncthread無法上傳文件。 。 。 而且沒有辦法將實際的File對象傳遞給表單嗎? 我不明白爲什麼通過瀏覽器工作,但不是從Android。 – lululoo 2013-03-10 01:10:51

回答

2

上傳文件到GAE Blob存儲區是一個兩個步驟:

  1. 首先你需要得到適當的URL在哪裏發表您的數據,人們通常使用類似「/ bloburl」處理器爲通用

  2. 當您有blob上傳URL時,您在請求中使用它。

  3. 您發送的文件不是NameValuePair,它應該是MultiPartEntity

這裏的作品(你需要的Apache HTTP庫MultiPartEntry支持)代碼:

DefaultHttpClient http_client = new DefaultHttpClient(); 
HttpGet http_get = new HttpGet(Config.BASE_URL + "bloburl"); 
HttpResponse response = http_client.execute(http_get); 
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
String first_line = reader.readLine(); 
Log.w(TAG, "blob_url: " + first_line); 

HttpPost post = new HttpPost(first_line); 
http_client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 

mime_type = "application/zip"; 
File file = new File(context.getFilesDir(), filename); 
entity.addPart("file", new FileBody(file, mime_type)); 
post.setEntity(entity); 

String result = EntityUtils.toString(http_client.execute(post).getEntity(), "UTF-8"); 
Log.i(TAG, result); 
+0

你能解釋一下第一個http_get/client在做什麼嗎?看起來它在某種程度上抓住了正確的上傳URL,但爲什麼這是必要的/通緝呢? – lululoo 2013-03-10 17:05:45

+0

^_ ^你先生是個天才。我現在看到第一個http_get抓取ACTUAL上傳網址。 – lululoo 2013-03-10 17:34:12