Tuesday, 26 April 2016

API parsing MyClientMultipartPost For sending Image to server Part 1

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/** * This is used for post data from API. * * @author Mayank * @since 1.4 */public class MyClientMultipartPost extends AsyncTask<Void, Void, Void> {

    private final String ApiLink;
    private final MultipartEntity multipartentity;
    public ProgressDialog dialog;
    private String message;
    private Context context;
    private OnPostCallComplete onpostcallcomplete;
    private JSONObject jsonResult;
    private String json;

    public MyClientMultipartPost(Context context, String progressMessage,
                                 OnPostCallComplete onPostCallComplete2, String ApiLink,
                                 MultipartEntity multipartentity) {
        message = progressMessage;
        this.context = context;
        this.ApiLink = ApiLink;
        this.multipartentity = multipartentity;
        this.onpostcallcomplete = onPostCallComplete2;

        if (!(message.equals(""))) {
            dialog = new ProgressDialog(context);
            dialog.setMessage(progressMessage);
        }
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub        super.onPreExecute();
        if (!(message.equals(""))) {
            dialog.setCancelable(false);
            dialog.show();
        }
    }

    @Override
    protected Void doInBackground(Void... params) {
        String result = null;

        if (!isCancelled()) {

            InputStream is = null;
            JSONObject jObj = null;

//            Utils.Log(getClass().getSimpleName(), "Url : " + ApiLink + " Params : " + multipartentity.toString());            System.out.println("create poll data " + ApiLink);
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpContext localContext = new BasicHttpContext();
                HttpPost httppost = new HttpPost(ApiLink);
                httppost.setEntity(multipartentity);

                HttpResponse response = httpclient.execute(httppost, localContext);
                if (response == null) {

                } else {
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();

                    // Log.e("log_tag", ""+is.toString());                }
            } catch (Exception e) {
                e.printStackTrace();
                // Log.e("log_tag", "Error in http connection " + e.toString());            }

            // convert response to string            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                json = sb.toString();
                Log.i("res", json);
            } catch (Exception e) {
                // Log.e("log_tag", "Error converting result " + e.toString());                e.printStackTrace();
            }
        }
        //System.out.println("result in post: "+result);        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub        super.onPostExecute(result);
        if (!(message.equals(""))) {
            if (dialog != null) {
                dialog.hide();
                dialog.dismiss();
            }
        }
        System.out.println("result in onPostExecute: " + result);
        try {
            onpostcallcomplete.response(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public interface OnPostCallComplete {
        public void response(String result) throws JSONException;
    }
}