我正在尝试使用 Sending images using Http Post 将图像发送到 REST 服务.这篇文章不再提供信息,但我在 FileBody bin1 = new FileBody(file);
处遇到编译错误。
错误:构造函数 FileBody(File[]) 未定义
这很奇怪,因为我定义了它,为什么会发生这种情况以及修复它的解决方案是什么? 非常感谢任何帮助。
代码来源:
private class ImageUpload extends AsyncTask<File, Void, String> {
@Override
protected void onPreExecute() {
if (checkNullState() == false) {
showMyDialog();
}
}
protected String doInBackground(File... file) {
String imageDescriptionTemp = "Photo Temp Description.";
String PostRequestUri = "https://demo.relocationmw.com/ws_docmgmt/Service1.svc";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(PostRequestUri);
FileBody bin1 = new FileBody(file);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("Image", bin1);
post.setEntity(entity);
HttpResponse response = client.execute(post);
resEntity = response.getEntity();
final String response_string = EntityUtils.toString(resEntity);
if(resEntity != null){
Log.i("RESPONSE", response_string);
}else{
return null;}
}
@Override
protected void onPostExecute(String result) {
if (checkNullState() == true) {
dismissMyDialog();
}
// add location once we have that figured out.
Toast.makeText(HomeActivity.this, "Image can be viewed {Location}",
Toast.LENGTH_LONG).show();
}
protected void onProgressUpdate(Map... values) {
}
最佳答案
我觉得你想要的是
FileBody bin1 = new FileBody(file[0]);
因为 doInBackground
使用 varargs .所以下面一行代码:
doInBackground(Params... params)
相当于
doInBackground(Params[] params)
当你这样做的时候
FileBody bin1 = new FileBody(file)
file
是一个数组(我想你已经像这样定义了构造函数 FileBody(File file)
)
关于android - AsynTask 中的文件类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15937003/