【发布时间】:2014-01-18 20:49:01
【问题描述】:
我正在尝试使用 http-client 从我的 Google Glass 将图像文件上传到我的服务器,但它总是卡在 httpclient.execute() 方法上。我不知道应该如何从我的 Glass 上传文件。这是我目前所拥有的:
httpClient = HttpUtils.getNewHttpsClient();
postRequest = new HttpPost(strURL);
final File file= new File("mnt/sdcard/DCIM/Camera/12232.jpg");
s = new StringBuilder();
try
{
if(file.exists())
{
final FileBody bin = new FileBody(file);
final MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uid", new StringBody(username,Charset.forName("UTF-8")));
reqEntity.addPart("pwd", new StringBody(password,Charset.forName("UTF-8")));
if(encKey!=null && !encKey.equals(""))
reqEntity.addPart("pvtkey", new StringBody(encKey,Charset.forName("UTF-8")));
reqEntity.addPart("p", new StringBody(selectedDrivePath,Charset.forName("UTF-8")));
reqEntity.addPart("ornt", new StringBody(fornt,Charset.forName("UTF-8")));
reqEntity.addPart("file_size", new StringBody(strfilesize,Charset.forName("UTF-8")));
reqEntity.addPart("data", bin);
contentLength=reqEntity.getContentLength();
postRequest.setEntity(reqEntity);
final HttpResponse response = httpClient.execute(postRequest);
...
}
...
}
...
我哪里出错了?
【问题讨论】:
-
您是否请求了
INTERNET权限,并且您是在后台线程中而不是在 UI 线程中发出请求吗? -
@TonyAllevato 是的,我正在使用 Internet 权限,并且所有内容都在服务中以异步任务运行。还有什么我需要添加的。
-
我刚刚注意到您的路径是
"mnt/sdcard/DCIM/Camera/12232.jpg",这将与您的应用程序运行所在的目录相关。不应该是"/mnt/..."吗? -
@TonyAllevato 我很抱歉路径以“/mnt/sdcard”开头,这只是一个错字。
-
我能解决这个问题..谢谢大家
标签: java google-glass google-gdk