1

Im trying to send an image to a REST service using Sending images using Http Post. The post is quit informative but i get a compiling error at FileBody bin1 = new FileBody(file);.

Error: The constructor FileBody(File[]) is undefined

Which is weird because i define it, why is this happenig and whats the solution to fixing it? Any help is greatly appreciated.

Code Source:

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) {
        }
Community
  • 1
  • 1
Keeano
  • 309
  • 8
  • 33

1 Answers1

1

I think that what you want is

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

beacuse doInBackground uses varargs. So the following line of code:

doInBackground(Params... params)

is equivalent to

doInBackground(Params[] params)

and when you do

FileBody bin1 = new FileBody(file)

file is an array (And I suppose that you have defined the constructor like this FileBody(File file))

Marcos
  • 4,643
  • 7
  • 33
  • 60
  • i get this error: Description Resource Path Location Type Syntax error, insert "}" to complete ClassBody HomeActivity.java /TestingApp/src/com/testingapp line 269 Java Problem – Keeano Apr 10 '13 at 23:41
  • I forgot to wrap it with a Try/Catch. My bad. Thanks for the Help! – Keeano Apr 11 '13 at 00:46