android - AsynTask 中的文件类型错误

标签 android rest android-asynctask apache-httpclient-4.x

我正在尝试使用 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/

相关文章:

安卓图片旋转

android - 使用 TextWatcher 在 EditText 中设置最大字数限制

android - 取消后再次运行异步任务,有什么办法吗?

java - 将 AsyncTask 重置为多次执行

java - 在 Apache Camel Rest 中使用路径变量

java - AsyncTask <Map<String, List<Object>>

java - 如何在 recyclerview 元素中设置固定的 CountDownTimer?

android - Retrofit2 的 onResponse() 和 onFailure() 方法中 first (Call<T> call) 参数的用途是什么

java - 好的做法? REST 中的一两个对象模型用于创建和获取资源

java - 来自响应的 Internet Explorer Cookie 不会覆盖现有 Cookie