Example usage for org.apache.http.entity.mime MultipartEntity consumeContent

List of usage examples for org.apache.http.entity.mime MultipartEntity consumeContent

Introduction

In this page you can find the example usage for org.apache.http.entity.mime MultipartEntity consumeContent.

Prototype

public void consumeContent() throws IOException, UnsupportedOperationException 

Source Link

Usage

From source file:org.opentraces.metatracker.net.OpenTracesClient.java

public void uploadFile(String fileName) {
    try {/* ww w. j av  a  2s.c  o  m*/
        DefaultHttpClient httpclient = new DefaultHttpClient();
        File f = new File(fileName);

        HttpPost httpost = new HttpPost("http://local.geotracing.com/tland/media.srv");
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("myIdentifier", new StringBody("somevalue"));
        entity.addPart("myFile", new FileBody(f));
        httpost.setEntity(entity);

        HttpResponse response;

        response = httpclient.execute(httpost);

        Log.d(LOG_TAG, "Upload result: " + response.getStatusLine());

        if (entity != null) {
            entity.consumeContent();
        }

        httpclient.getConnectionManager().shutdown();

    } catch (Throwable ex) {
        Log.d(LOG_TAG, "Upload failed: " + ex.getMessage() + " Stacktrace: " + ex.getStackTrace());
    }

}

From source file:org.rapidandroid.activity.FormReviewer.java

private void uploadFile(final String filename) {
    Toast.makeText(getApplicationContext(), FILE_UPLOAD_BEGUN, Toast.LENGTH_LONG).show();
    Thread t = new Thread() {
        @Override/*from w w  w .  j  a  va 2  s . c  o m*/
        public void run() {
            try {
                DefaultHttpClient httpclient = new DefaultHttpClient();

                File f = new File(filename);

                HttpPost httpost = new HttpPost("http://192.168.7.127:8160/upload/upload");
                MultipartEntity entity = new MultipartEntity();
                entity.addPart("myIdentifier", new StringBody("somevalue"));
                entity.addPart("myFile", new FileBody(f));
                httpost.setEntity(entity);

                HttpResponse response;

                // Post, check and show the result (not really spectacular,
                // but works):
                response = httpclient.execute(httpost);

                Log.d("httpPost", "Login form get: " + response.getStatusLine());

                if (entity != null) {
                    entity.consumeContent();
                }

                success = true;
            } catch (Exception ex) {
                Log.d("FormReviewer",
                        "Upload failed: " + ex.getMessage() + " Stacktrace: " + ex.getStackTrace());
                success = false;
            } finally {
                mDebugHandler.post(mFinishUpload);
            }
        }
    };
    t.start();
}

From source file:net.ccghe.emocha.async.UploadOneFile.java

public UploadOneFile(String path, String serverURL, FileTransmitter transmitter, MultipartEntity postData) {
    // configure connection
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 20 * Constants.ONE_SECOND);
    HttpConnectionParams.setSoTimeout(params, 20 * Constants.ONE_SECOND);
    HttpClientParams.setRedirecting(params, false);

    // setup client
    DefaultHttpClient client = new DefaultHttpClient(params);

    HttpPost post = new HttpPost(serverURL);

    int id = 0;/*from w  ww. j  a va2s. co m*/
    postData.addPart("file" + id, new FileBody(new File(path)));
    try {
        postData.addPart("path" + id, new StringBody(path));
    } catch (UnsupportedEncodingException e1) {
        Log.e(Constants.LOG_TAG, "Encoding error while uploading file.");
    }

    // prepare response and return uploaded
    try {
        post.setEntity(postData);
        HttpResponse response = client.execute(post);

        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        String jsonResponse = Server.convertStreamToString(stream);
        stream.close();
        if (postData != null) {
            postData.consumeContent();
        }
        JSONObject jObject = new JSONObject(jsonResponse);

        Long ok = jObject.getLong("ok");

        if (ok > 0) {
            DBAdapter.markAsUploaded(path);
            Log.i(Constants.LOG_TAG, "Mark as uploaded: " + path);
        } else {
            Log.e(Constants.LOG_TAG, "Error uploading: " + path + " (json response not ok)");
        }
    } catch (ClientProtocolException e) {
        Log.e("EMOCHA", "ClientProtocolException ERR. " + e.getMessage());
    } catch (IOException e) {
        Log.e("EMOCHA", "IOException ERR. " + e.getMessage());
    } catch (Exception e) {
        Log.e("EMOCHA", "Exception ERR. " + e.getMessage());
    }

    /*
    // check response.
    // TODO: This isn't handled correctly.
    String serverLocation = null;
    Header[] h = response.getHeaders("Location");
    if (h != null && h.length > 0) {
       serverLocation = h[0].getValue();
    } else {
       // something should be done here...
       Log.e(Constants.LOG_TAG, "Location header was absent");
    }
    int responseCode = response.getStatusLine().getStatusCode();
    Log.e(Constants.LOG_TAG, "Response code:" + responseCode);
            
    // verify that your response came from a known server
    if (serverLocation != null && serverURL.contains(serverLocation)
    && responseCode == 201) {
       DBAdapter.markAsUploaded(path);
    }
    */
    transmitter.transmitComplete();
}

From source file:com.siahmsoft.soundroid.sdk7.services.UploadService.java

public void uploadTrack(Bundle bundle) {

    Track track = TracksStore.Track.fromBundle(bundle);
    //SystemClock.sleep(1500);

    if (track.getmIdTrack() == 0) {

        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null,
                Charset.forName(HTTP.UTF_8));

        try {//from w w w  . ja v a 2s .c o  m

            if (track.getmTrackPath() != null && !"".equals(track.getmTrackPath())) {
                ContentBody asset_Data = new FileBody(new File(track.getmTrackPath()));
                entity.addPart("track[asset_data]", asset_Data);
            }

            if (track.getmArtworkPath() != null && !"".equals(track.getmArtworkPath())) {
                ContentBody artworkData = new FileBody(new File(track.getmArtworkPath()));
                entity.addPart("track[artwork_data]", artworkData);
            }

            if (track.getmTitle() != null && !"".equals(track.getmTitle())) {
                entity.addPart("track[title]", new StringBody(track.getmTitle()));
            }

            if (track.getmDescription() != null && !"".equals(track.getmDescription())) {
                entity.addPart("track[description]", new StringBody(track.getmDescription()));
            }

            if (track.getmDownloadable() != null && !"".equals(track.getmDownloadable())) {
                entity.addPart("track[downloadable]", new StringBody(track.getmDownloadable()));
            }

            if (track.getmSharing() != null && !"".equals(track.getmSharing())) {
                entity.addPart("track[sharing]", new StringBody(track.getmSharing()));
            }

            if (!"".equals(track.getmBpm())) {
                entity.addPart("track[bpm]", new StringBody(String.valueOf(track.getmBpm())));
            }

            if (track.getmTagList() != null && !"".equals(track.getmTagList())) {
                entity.addPart("track[tag_list]", new StringBody(track.getmTagList()));
            }

            if (track.getmGenre() != null && !"".equals(track.getmGenre())) {
                entity.addPart("track[genre]", new StringBody(track.getmGenre()));
            }

            if (track.getmLicense() != null && !"".equals(track.getmLicense())) {
                entity.addPart("track[license]", new StringBody(track.getmLicense()));
            }

            if (track.getmLabelName() != null && !"".equals(track.getmLabelName())) {
                entity.addPart("track[label_name]", new StringBody(track.getmLabelName()));
            }

            if (track.getmTrackType() != null && !"".equals(track.getmTrackType())) {
                entity.addPart("track[track_type]", new StringBody(track.getmTrackType()));
            }

            HttpPost filePost = new HttpPost("http://api.soundcloud.com/tracks");
            Soundroid.getSc().signRequest(filePost);
            filePost.setEntity(entity);

            try {
                //SystemClock.sleep(1000);
                showNotification("Uploading track " + track.getmTitle());
                final HttpResponse response = sClient.execute(filePost);
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
                    showNotification("Track " + track.getmTitle() + " uploaded");
                    sClient.getConnectionManager().closeExpiredConnections();
                    totalUploads--;
                }
            } finally {
                if (entity != null) {
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {

        }
    } else {//Edicin de la cancin

        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null,
                Charset.forName(HTTP.UTF_8));
        try {

            if (track.getmTrackPath() != null && !"".equals(track.getmTrackPath())) {
                ContentBody asset_Data = new FileBody(new File(track.getmTrackPath()));
                entity.addPart("track[asset_data]", asset_Data);
            }

            if (track.getmArtworkPath() != null && !"".equals(track.getmArtworkPath())) {
                ContentBody artworkData = new FileBody(new File(track.getmArtworkPath()));
                entity.addPart("track[artwork_data]", artworkData);
            }

            if (track.getmTitle() != null) {
                entity.addPart("track[title]", new StringBody(track.getmTitle()));
            }

            if (track.getmDescription() != null) {
                entity.addPart("track[description]", new StringBody(track.getmDescription()));
            }

            if (track.getmDownloadable() != null) {
                entity.addPart("track[downloadable]", new StringBody(track.getmDownloadable()));
            }

            if (track.getmSharing() != null) {
                entity.addPart("track[sharing]", new StringBody(track.getmSharing()));
            }

            entity.addPart("track[bpm]", new StringBody(String.valueOf(track.getmBpm())));

            if (track.getmTagList() != null) {
                entity.addPart("track[tag_list]", new StringBody(track.getmTagList()));
            }

            if (track.getmGenre() != null) {
                entity.addPart("track[genre]", new StringBody(track.getmGenre()));
            }

            if (track.getmLicense() != null) {
                entity.addPart("track[license]", new StringBody(track.getmLicense()));
            }

            if (track.getmLabelName() != null) {
                entity.addPart("track[label_name]", new StringBody(track.getmLabelName()));
            }

            if (track.getmTrackType() != null) {
                entity.addPart("track[track_type]", new StringBody(track.getmTrackType()));
            }

            HttpPut filePut = new HttpPut("http://api.soundcloud.com/tracks/" + track.getmIdTrack() + ".json");
            Soundroid.getSc().signRequest(filePut);

            filePut.setEntity(entity);

            try {
                //SystemClock.sleep(1000);
                showNotification("Uploading track " + track.getmTitle());
                final HttpResponse response = sClient.execute(filePut);
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
                    showNotification("Track " + track.getmTitle() + " edited");
                    sClient.getConnectionManager().closeExpiredConnections();
                }
            } finally {
                if (entity != null) {
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {

        }
    }
}