2013-03-09 66 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