Android Open Source - unbounce-android Network Helper






From Project

Back to project page unbounce-android.

License

The source code is released under:

MIT License

If you think the Android project unbounce-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.ryansteckler.nlpunbounce.helpers;
//from   ww w  .  j a  v  a2 s .c o  m
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * Created by rsteckler on 10/1/14.
 */
public class NetworkHelper{

    private NetworkHelper(){}
    public static void getFromServer(final String url, final Handler handler) {
        //Post the content to the URL

        new Thread()
        {
            @Override
            public void run() {

                HttpClient httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet(url);

                try {
                    // Execute HTTP Get Request
                    HttpResponse response = httpclient.execute(httpget);
                    if (response.getStatusLine().getStatusCode() < 300) {
                        Message msg = new Message();
                        msg.what = 1;
                        Bundle b = new Bundle();
                        String responseStr = EntityUtils.toString(response.getEntity());
                        b.putString("global_stats", responseStr);
                        msg.setData(b);
                        handler.sendMessage(msg);
                        return;
                    }

                } catch (ClientProtocolException e) {
                } catch (IOException e) {
                } catch (SecurityException e) {
                    //We were denied internet permissions.
                }
                handler.sendEmptyMessage(0);

            }
        }.start();
    }

    public static void sendToServer(final String contentId, final String content, final String url, final Handler handler) {
        //Post the content to the URL

        new Thread()
        {
            @Override
            public void run() {

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);

                try {
                    // Add data
                    httppost.setEntity(new  StringEntity(content));

                    // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);
                    if (response.getStatusLine().getStatusCode() < 300) {
                        Message msg = new Message();
                        msg.what = 1;
                        Bundle b = new Bundle();
                        String responseStr = EntityUtils.toString(response.getEntity());
                        b.putString("global_stats", responseStr);
                        msg.setData(b);
                        handler.sendMessage(msg);
                        return;
                    }

                } catch (ClientProtocolException e) {
                } catch (IOException e) {
                } catch (SecurityException e) {
                    //We were denied internet permissions.
                }
                handler.sendEmptyMessage(0);

            }
        }.start();
    }
}




Java Source Code List

com.example.rsteckler.myapplication.ApplicationTest.java
com.ryansteckler.inappbilling.Base64DecoderException.java
com.ryansteckler.inappbilling.Base64.java
com.ryansteckler.inappbilling.IabException.java
com.ryansteckler.inappbilling.IabHelper.java
com.ryansteckler.inappbilling.IabResult.java
com.ryansteckler.inappbilling.Inventory.java
com.ryansteckler.inappbilling.Purchase.java
com.ryansteckler.inappbilling.Security.java
com.ryansteckler.inappbilling.SkuDetails.java
com.ryansteckler.nlpunbounce.ActivityReceiver.java
com.ryansteckler.nlpunbounce.AlarmDetailFragment.java
com.ryansteckler.nlpunbounce.AlarmsFragment.java
com.ryansteckler.nlpunbounce.BaseDetailFragment.java
com.ryansteckler.nlpunbounce.ExpandingLayout.java
com.ryansteckler.nlpunbounce.HomeFragment.java
com.ryansteckler.nlpunbounce.MaterialSettingsActivity.java
com.ryansteckler.nlpunbounce.NavigationDrawerFragment.java
com.ryansteckler.nlpunbounce.ServiceDetailFragment.java
com.ryansteckler.nlpunbounce.ServicesFragment.java
com.ryansteckler.nlpunbounce.SettingsActivity.java
com.ryansteckler.nlpunbounce.WakelockDetailFragment.java
com.ryansteckler.nlpunbounce.WakelocksFragment.java
com.ryansteckler.nlpunbounce.XposedReceiver.java
com.ryansteckler.nlpunbounce.adapters.AlarmsAdapter.java
com.ryansteckler.nlpunbounce.adapters.BaseAdapter.java
com.ryansteckler.nlpunbounce.adapters.ServicesAdapter.java
com.ryansteckler.nlpunbounce.adapters.WakelocksAdapter.java
com.ryansteckler.nlpunbounce.helpers.DownloadHelper.java
com.ryansteckler.nlpunbounce.helpers.LocaleHelper.java
com.ryansteckler.nlpunbounce.helpers.LogHelper.java
com.ryansteckler.nlpunbounce.helpers.NetworkHelper.java
com.ryansteckler.nlpunbounce.helpers.RootHelper.java
com.ryansteckler.nlpunbounce.helpers.SettingsHelper.java
com.ryansteckler.nlpunbounce.helpers.SortWakeLocks.java
com.ryansteckler.nlpunbounce.helpers.ThemeHelper.java
com.ryansteckler.nlpunbounce.helpers.UidNameResolver.java
com.ryansteckler.nlpunbounce.hooks.Wakelocks.java
com.ryansteckler.nlpunbounce.models.AlarmStats.java
com.ryansteckler.nlpunbounce.models.BaseStatsWrapper.java
com.ryansteckler.nlpunbounce.models.BaseStats.java
com.ryansteckler.nlpunbounce.models.EventLookup.java
com.ryansteckler.nlpunbounce.models.InterimEvent.java
com.ryansteckler.nlpunbounce.models.ServiceStats.java
com.ryansteckler.nlpunbounce.models.UnbounceStatsCollection.java
com.ryansteckler.nlpunbounce.models.WakelockStats.java
com.ryansteckler.nlpunbounce.tasker.TaskerActivity.java
com.ryansteckler.nlpunbounce.tasker.TaskerReceiver.java
com.ryansteckler.nlpunbounce.tasker.TaskerWhichFragment.java