Question

I changed the HttpPost that I have for security.

I am able to get the app to load/take picture, but it can't talk to the API, because the API is using a different retrieval method.

I do have an example of multipart coding but unsure how to implement with this. An example of what I'm doing/what I need to do can be found here.

How would I implement using multipart form data?

 protected Object doInBackground(Object... arg0) {
    Bitmap bitmapOrg = ((Main) parentActivity).mPhoto;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // compress bitmap into the byte array output stream
    bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 75, baos);
    // form byte array out of byte array output stream
    byte[] ba = baos.toByteArray();
    String encodedImage = Base64.encodeBytes(ba);
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(
            "-----------------------------------");
    // Add your data


    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("image", encodedImage));
    try {
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream inputstream = entity.getContent();
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(inputstream));
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        results = sb.toString();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}
Was it helpful?

Solution

First, you have to start by converting using the multi-part method.

    Bitmap bitmapOrg = ((Main) parentActivity).mPhoto;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // compress bitmap into the byte array output stream
    bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 75, baos);
    // form byte array out of byte array output stream
    byte[] ba = baos.toByteArray();
    String encodedImage = Base64.encodeBytes(ba);
    HttpClient httpclient = new DefaultHttpClient();

By going to this, which is actually in stackoverflow, Post multipart request with Android SDK, provide key examples and samples of how multi-part works.

HttpPost httppost = new HttpPost("some url");

    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
    multipartEntity.addPart("Title", new StringBody("Title"));
    multipartEntity.addPart("Nick", new StringBody("Nick"));
    multipartEntity.addPart("Email", new StringBody("Email"));
    multipartEntity.addPart("Description", new StringBody(Settings.SHARE.TEXT));
    multipartEntity.addPart("Image", new FileBody(image));
    httppost.setEntity(multipartEntity);

    mHttpClient.execute(httppost, new PhotoUploadResponseHandler());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top