Android Open Source - photo-share-android Multipart Json Request






From Project

Back to project page photo-share-android.

License

The source code is released under:

Apache License

If you think the Android project photo-share-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.project.photoshare.api;
/*from  w  w  w. j  a v  a  2  s.  co  m*/
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonRequest;
import com.project.photoshare.utils.ImageUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * <p>Request class to post multipart contents.</p>
 *
 * Created by TakuyaKodama on 14/06/08.
 */
public class MultipartJsonRequest extends JsonRequest<JSONObject> {
    private static final String TAG = MultipartJsonRequest.class.getSimpleName();

    private Map<String, String> mStringParams;
    private Map<String, ?> mBinaryParams;
    private MultipartEntityBuilder mMultipartEntityBuilder = MultipartEntityBuilder.create();

    public MultipartJsonRequest(String url, Map<String, String> stringParams, Map<String, ?> bynaryParams,
                                Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        super(Method.POST, url, null, listener, errorListener);

        mStringParams = stringParams;
        mBinaryParams = bynaryParams;
        buildMultipartEntity();
    }

    private void buildMultipartEntity() {
        mMultipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        mMultipartEntityBuilder.setBoundary("___________________" + Long.toString(System.currentTimeMillis()));
        mMultipartEntityBuilder.setCharset(Consts.UTF_8);

        for (Map.Entry<String, String> entry : mStringParams.entrySet()) {
            mMultipartEntityBuilder.addTextBody(entry.getKey(), entry.getValue());
        }

        for (Map.Entry<String, ?> entry : mBinaryParams.entrySet()) {
            ContentType imageContentType = ContentType.create("image/jpeg");

            if (entry.getValue() instanceof byte[]) {
                Log.d(TAG, "entry.getValue() => byte[]");
                mMultipartEntityBuilder.addBinaryBody("uploadFiles", (byte[]) entry.getValue(), imageContentType, entry.getKey());
            } else if (entry.getValue() instanceof File) {
                Log.d(TAG, "entry.getValue() => File");
                mMultipartEntityBuilder.addBinaryBody("uploadFiles", (File) entry.getValue(), imageContentType, entry.getKey());
            } else if (entry.getValue() instanceof InputStream) {
                Log.d(TAG, "entry.getValue() => InputStream");
                Log.d(TAG, "key: " + entry.getKey());
                Bitmap bitmap = BitmapFactory.decodeStream( (InputStream) entry.getValue() );
                byte[] bitmapByteArray = ImageUtils.getResizedByteArray(bitmap);
                mMultipartEntityBuilder.addBinaryBody("uploadFiles[]", bitmapByteArray, imageContentType, entry.getKey());
            }
        }
    }

    @Override
    public String getBodyContentType() {
        return mMultipartEntityBuilder.build().getContentType().getValue();
    }

    public HttpEntity getEntity() {

        return mMultipartEntityBuilder.build();
    }

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        byte[] responseData = response.data;
        try {
            String jsonString = new String(responseData, HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return Response.error(new ParseError(e));
        } catch (JSONException e) {
            e.printStackTrace();
            Log.d(TAG, new String(responseData));
            return Response.error(new ParseError(e));
        }
    }
}




Java Source Code List

com.project.photoshare.DefaultActivity.java
com.project.photoshare.DownloadPageFragment.java
com.project.photoshare.HistoryPageFragment.java
com.project.photoshare.InputPassword.java
com.project.photoshare.MainActivity.java
com.project.photoshare.MyPageFragment.java
com.project.photoshare.PasswordConfirmation.java
com.project.photoshare.SelectIsAddLater.java
com.project.photoshare.SplashActivity.java
com.project.photoshare.UploadCompleted.java
com.project.photoshare.UploadPageFragment.java
com.project.photoshare.api.ApiRequestService.java
com.project.photoshare.api.InputStreamRequest.java
com.project.photoshare.api.MultipartHurlStack.java
com.project.photoshare.api.MultipartJsonRequest.java
com.project.photoshare.listener.PostImageErrorListener.java
com.project.photoshare.listener.PostImageSuccessListener.java
com.project.photoshare.utils.BitmapDecodeLoader.java
com.project.photoshare.utils.ImageUtils.java
com.project.photoshare.utils.LogHelper.java
com.project.photoshare.utils.Utils.java
com.project.photoshare.utils.volley.LruImageCache.java
com.project.photoshare.utils.volley.VolleyHelper.java
com.project.photoshare.widget.CustomEditText.java