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

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

Introduction

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

Prototype

public MultipartEntity() 

Source Link

Usage

From source file:fm.smart.r1.ItemActivity.java

public AddImageResult addImage(File file, String media_entity, String author, String author_url,
        String attribution_license_id, String sentence_id, String item_id, String list_id) {
    String http_response = "";
    int status_code = 0;
    HttpClient client = null;//from w w w .  ja  va  2s.c  o m
    try {
        client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://api.smart.fm/lists/" + list_id + "/items/" + item_id
                + "/sentences/" + sentence_id + "/images");
        // HttpPost post = new HttpPost("http://api.smart.fm/sentences/" +
        // sentence_id
        // + "/images");

        String auth = LoginActivity.username(this) + ":" + LoginActivity.password(this);
        byte[] bytes = auth.getBytes();
        post.setHeader("Authorization", "Basic " + new String(Base64.encodeBase64(bytes)));

        // httppost.setHeader("Content-Type",
        // "application/x-www-form-urlencoded");

        post.setHeader("Host", "api.smart.fm");

        FileBody bin = new FileBody(file, "image/jpeg");
        StringBody media_entity_part = new StringBody(media_entity);
        StringBody author_part = new StringBody(author);
        StringBody author_url_part = new StringBody(author_url);
        StringBody attribution_license_id_part = new StringBody(attribution_license_id);
        StringBody sentence_id_part = new StringBody(sentence_id);
        StringBody api_key_part = new StringBody(Main.API_KEY);
        StringBody item_id_part = new StringBody(item_id);
        StringBody list_id_part = new StringBody(list_id);

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("image[file]", bin);
        reqEntity.addPart("media_entity", media_entity_part);
        reqEntity.addPart("author", author_part);
        reqEntity.addPart("author_url", author_url_part);
        reqEntity.addPart("attribution_license_id", attribution_license_id_part);
        reqEntity.addPart("sentence_id", sentence_id_part);
        reqEntity.addPart("api_key", api_key_part);
        reqEntity.addPart("item_id", item_id_part);
        reqEntity.addPart("list_id", list_id_part);

        post.setEntity(reqEntity);

        Header[] array = post.getAllHeaders();
        for (int i = 0; i < array.length; i++) {
            Log.d("DEBUG", array[i].toString());
        }

        Log.d("AddImage", "executing request " + post.getRequestLine());
        HttpResponse response = client.execute(post);
        HttpEntity resEntity = response.getEntity();
        status_code = response.getStatusLine().getStatusCode();

        Log.d("AddImage", "----------------------------------------");
        Log.d("AddImage", response.getStatusLine().toString());
        array = response.getAllHeaders();
        for (int i = 0; i < array.length; i++) {
            Log.d("AddImage", array[i].toString());
        }
        if (resEntity != null) {
            Log.d("AddImage", "Response content length: " + resEntity.getContentLength());
            Log.d("AddImage", "Chunked?: " + resEntity.isChunked());
        }
        long length = response.getEntity().getContentLength();
        byte[] response_bytes = new byte[(int) length];
        response.getEntity().getContent().read(response_bytes);
        Log.d("AddImage", new String(response_bytes));
        http_response = new String(response_bytes);
        if (resEntity != null) {
            resEntity.consumeContent();
        }

        // HttpEntity entity = response1.getEntity();
    } catch (IOException e) {
        /* Reset to Default image on any error. */
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return new AddImageResult(status_code, http_response);
}

From source file:de.geomobile.joined.api.service.JOWebService.java

/**
 * User logout./* w w w .j  av  a 2  s  . c  om*/
 * 
 * @param userId
 *            The users id.
 * 
 * @param secureToken
 *            The users secureToken.
 * @throws JOFriendFinderUnexpectedException
 * @throws JOFriendFinderServerException
 * @throws JOFriendFinderHTTPException
 */
public void logoutUser(String userId, String secureToken)
        throws JOFriendFinderUnexpectedException, JOFriendFinderServerException, JOFriendFinderHTTPException {
    try {
        HttpClient httpClient = getNewHttpClient();
        HttpPut httpput = new HttpPut(getJoinedServerUrl() + JOConfig.FF_FRIENDS + "/" + userId);
        httpput.addHeader(getAuthenticationHeader(userId, secureToken));
        MultipartEntity entity = new MultipartEntity();
        entity.addPart(JOConfig.REGISTRATION_ID, new StringBody("", Charset.forName(JOConfig.UTF_8)));
        entity.addPart(JOConfig.IS_ACTIVE,
                new StringBody(String.valueOf(false), Charset.forName(JOConfig.UTF_8)));
        httpput.setEntity(entity);
        HttpResponse httpResponse = httpClient.execute(httpput);
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
            throw new JOFriendFinderServerException();
        } else if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT) {
            throw new JOFriendFinderUnexpectedException(
                    "HTTP Error Code " + httpResponse.getStatusLine().getStatusCode());
        }
    } catch (UnsupportedEncodingException e) {
        throw new JOFriendFinderUnexpectedException(e);
    } catch (ClientProtocolException e) {
        throw new JOFriendFinderHTTPException(e);
    } catch (IOException e) {
        throw new JOFriendFinderHTTPException(e);
    }
}

From source file:bluej.collect.DataCollectorImpl.java

public static void invokeCompileError(Package pkg, String code, String compilationError) {
    if (!false) {
        MultipartEntity mpe = new MultipartEntity();

        mpe.addPart("event[invoke][code]", CollectUtility.toBody(code));
        mpe.addPart("event[invoke][result]", CollectUtility.toBody("compile_error"));
        mpe.addPart("event[invoke][compile_error]", CollectUtility.toBody(compilationError));
        submitEvent(pkg.getProject(), pkg, EventName.INVOKE_METHOD, new PlainEvent(mpe));
    }/*ww  w .j ava  2s  . c o  m*/
}