Android Open Source - mobile-android Service Handler






From Project

Back to project page mobile-android.

License

The source code is released under:

MIT License

If you think the Android project mobile-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.manyconf.conference;
/*from   w  ww .ja va 2 s .  com*/
/**
 * Class to handle all HTTP calls. This class is responsible of making a HTTP call and getting the
 * response.
 *
 * See also:
 * http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
 * And:
 * http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android
 */

import android.app.Activity;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

 public class ServiceHandler {

    static String response = null;
    public final static int GET = 1;
    public final static int POST = 2;

    /**
     * Making service call
     * @url - url to make request
     * @method - http request method
     * */
    public String makeServiceCall(String url, int method) throws IOException {
        return this.makeServiceCall(url, method, null);
    }

    /**
     * Making service call.
     * @url - url to make request
     * @method - http request method
     * @params - http request params
     * */
    public String makeServiceCall(String url, int method,
                                  List<NameValuePair> params) throws IOException {
        Log.d("manyconf.serv", "makeServiceCall()");

        try {
            this.response = null;

            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // Checking http request method type
            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                // adding post params
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }

                httpResponse = httpClient.execute(httpPost);

            } else if (method == GET) {
                // appending params to url
                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                }
                HttpGet httpGet = new HttpGet(url);
                httpGet.setHeader("Accept", "application/json");


                httpResponse = httpClient.execute(httpGet);

            }
            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            Log.e("manyconf.serv", e.getLocalizedMessage());
            throw e;
        } catch (ClientProtocolException e) {
            Log.e("manyconf.serv", e.getLocalizedMessage());
            throw e;
        } catch (IOException e) {
            Log.e("manyconf.serv", e.getLocalizedMessage());
            throw e;
        }

        return response;
    }

}




Java Source Code List

com.manyconf.conference.ConferenceApplication.java
com.manyconf.conference.ConferenceBuilderDelegate.java
com.manyconf.conference.ConferenceBuilder.java
com.manyconf.conference.ConferenceDetailActivity.java
com.manyconf.conference.ConferenceListActivity.java
com.manyconf.conference.ConferenceModel.java
com.manyconf.conference.Const.java
com.manyconf.conference.IsoDateParser.java
com.manyconf.conference.MainActivity.java
com.manyconf.conference.PresentationActivity.java
com.manyconf.conference.PresentationModel.java
com.manyconf.conference.PrivateConst.java
com.manyconf.conference.ReadFile.java
com.manyconf.conference.ScheduleActivity.java
com.manyconf.conference.ServiceHandler.java
com.manyconf.conference.SpeakerActivity.java
com.manyconf.conference.SpeakerListActivity.java
com.manyconf.conference.SpeakerModel.java
com.manyconf.conference.Theme.java
com.manyconf.conference.ThemedArrayAdapter.java
com.manyconf.conference.TrackModel.java