文件类型中的AsynTask出错

时间:2013-04-10 21:44:17

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

我尝试使用Sending images using Http Post将图像发送到REST服务。该帖子已退出信息,但我在FileBody bin1 = new FileBody(file);收到了编译错误。

错误:The constructor FileBody(File[]) is undefined

这很奇怪,因为我定义了它,为什么会发生这种情况并解决它的问题? 非常感谢任何帮助。

代码来源:

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) {
        }

1 个答案:

答案 0 :(得分:1)

我认为你想要的是

FileBody bin1 = new FileBody(file[0]);

beacuse doInBackground使用varargs。所以下面的代码行:

doInBackground(Params... params)

相当于

doInBackground(Params[] params)

当你做

FileBody bin1 = new FileBody(file)

file是一个数组(我想你已经定义了像FileBody(File file)这样的构造函数)