我在上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的浏览器中点击“提交”?
它抛出什么异常?请发布堆栈跟踪。 – wtsang02 2013-03-10 00:02:18
请谷歌。知道为什么会发生NetworkOnMainThreadException,那么你应该找到你的答案。 – wtsang02 2013-03-10 00:52:25
我已经将它改回(AsyncThread)。它不会抛出异常。无论出于何种原因,我只能等待很长时间才能运行后台进程。无论哪种方式,似乎我的文件没有被上传。 当通过浏览器为文件填写表格时,表格是否填写为本地计算机中文件的目录?如果是这种情况,我不明白为什么我当前的asyncthread无法上传文件。 。 。 而且没有办法将实际的File对象传递给表单吗? 我不明白为什么通过浏览器工作,但不是从Android。 – lululoo 2013-03-10 01:10:51