com.urucas.services.JSONFileRequest.java Source code

Java tutorial

Introduction

Here is the source code for com.urucas.services.JSONFileRequest.java

Source

package com.urucas.services;

/**
 * @copyright Urucas
 * @license   Copyright (C) 2013. All rights reserved
 * @version   Release: 1.0.0
 * @link       http://urucas.com
 * @developers Bruno Alassia, Pamela Prosperi
 */

import android.util.Log;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class JSONFileRequest extends RequestTask {

    public JSONFileRequest(JSONRequestTaskHandler _jrsh) {
        super(_jrsh);
    }

    private File file;

    public RequestTask setFile(File file) {
        this.file = file;
        return this;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected String doInBackground(String... uri) {

        HttpClient httpclient = new DefaultHttpClient();

        // add request params
        StringBuffer url = new StringBuffer(uri[0]).append("?")
                .append(URLEncodedUtils.format(getParams(), "UTF-8"));

        HttpPost _post = new HttpPost(url.toString());
        HttpContext localContext = new BasicHttpContext();

        Log.i("url ---->", url.toString());

        MultipartEntity _entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        for (int index = 0; index < getParams().size(); index++) {
            try {
                _entity.addPart(getParams().get(index).getName(),
                        new StringBody(getParams().get(index).getValue()));
            } catch (UnsupportedEncodingException e) {
            }
        }

        _entity.addPart("imagen", new FileBody(file));

        _post.setEntity(_entity);

        HttpResponse response;

        String responseString = null;
        // execute
        try {
            response = httpclient.execute(_post, localContext);
            StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
                if (isCancelled()) {
                    rsh.onError("Task cancel");
                }

            } else {
                //Closes the connection.
                response.getEntity().getContent().close();
                Log.i("status", String.valueOf(statusLine.getStatusCode()));
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            //rsh.onError(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            //rsh.onError(e.getMessage());      
        }
        return responseString;
    }

    protected void onPostExecute(String result) {

        if (result == null) {
            jrsh.onError("Cant parse JSON");
            return;
        }
        Log.i("api result", result);
        try {
            Object json = new JSONTokener(result).nextValue();
            if (json instanceof JSONObject) {
                JSONObject response = new JSONObject(result);
                jrsh.onSuccess(response);
                return;
            } else if (json instanceof JSONArray) {
                JSONArray response = new JSONArray(result);
                jrsh.onSuccess(response);
                return;
            }
            jrsh.onError("Cant parse JSON");

        } catch (JSONException e) {
            jrsh.onError(e.getMessage());
        }
    }

}