0

Actually I'm developing the app that send submit form as name-value pair using json for server communication . But the problem is now I want to send the Image or text file during form submission ,how shall I send the image or text file during form submission . Is there any correct procedure for this?

JCodes13
  • 488
  • 5
  • 17
Strawberry
  • 667
  • 2
  • 10
  • 24

2 Answers2

1

Try below code for Image Upload to server:

private class ImageUploader extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub
            String result = "";

            // Client-side HTTP transport library
            HttpClient httpClient = new DefaultHttpClient();

            // using POST method
            HttpPost httpPostRequest = new HttpPost(imagePostUrl);
            try {

                // creating a file body consisting of the file that we want to
                // send to the server
                FileBody bin = new FileBody(imageFile);

                /**
                 * An HTTP entity is the majority of an HTTP request or
                 * response, consisting of some of the headers and the body, if
                 * present. It seems to be the entire request or response
                 * without the request or status line (although only certain
                 * header fields are considered part of the entity).
                 * 
                 * */
                MultipartEntityBuilder multiPartEntityBuilder = MultipartEntityBuilder.create();
                multiPartEntityBuilder.addPart("images[1]", bin);
                httpPostRequest.setEntity(multiPartEntityBuilder.build());

                // Execute POST request to the given URL
                HttpResponse httpResponse = null;
                httpResponse = httpClient.execute(httpPostRequest);

                // receive response as inputStream
                InputStream inputStream = null;
                inputStream = httpResponse.getEntity().getContent();

                if (inputStream != null)
                    result = convertInputStreamToString(inputStream);
                else
                    result = "Did not work!";
                return result;
            } catch (Exception e) {

                return null;
            }

            // return result;
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            uploadStatus.setText("Uploading image to server");
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            uploadStatus.setText(result);
        }

    }

    private static String convertInputStreamToString(InputStream inputStream)
            throws IOException {
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while ((line = bufferedReader.readLine()) != null)
            result += line;

        inputStream.close();
        return result;

    }
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
  • @pratik MultipartEntityBuilder cannot resolved to a type. Is MultipartEntityBuilder will work in android 2.3.3? – Strawberry May 15 '14 at 11:37
  • @Strawberry you have to download httpclient and httpmime jar files and need to put in lib folder and after need to add to build path. After that you will get this all classes for use. – Pratik Dasa May 15 '14 at 11:38
0

For file try below code:

String url = "http://yourserver";
File file = new File(Environment.getExternalStorageDirectory(),
        "yourfile");
try {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(url);

    InputStreamEntity reqEntity = new InputStreamEntity(
            new FileInputStream(file), -1);
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(true); // Send in multiple parts if needed
    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);
    //Do something with response...

} catch (Exception e) {
    // show error
}
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44