Android Open Source - sphincter-remote Sphincter Request Handler






From Project

Back to project page sphincter-remote.

License

The source code is released under:

GNU General Public License

If you think the Android project sphincter-remote 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.michiwend.spincterremote;
/* ww  w.j  ava 2s.com*/

import android.content.SharedPreferences;
import android.os.AsyncTask;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

enum Action {
    open_door,
    close_door,
    update_state
}

public class SphincterRequestHandler extends AsyncTask<Action, Void, String> {

    private OnTaskCompleted listener;
    private SharedPreferences sharedPreferences;

    public SphincterRequestHandler(OnTaskCompleted l, SharedPreferences p){
        this.listener = l;
        this.sharedPreferences = p;
    }

    @Override
    protected String doInBackground(Action... params) {
        String url = sharedPreferences.getString("prefURL", "");

        switch (params[0]) {
            case open_door:
                url += "?action=open";
                break;
            case close_door:
                url += "?action=close";
                break;
            case update_state:
                url += "?action=state";
        }

        url += "&token=" + sharedPreferences.getString("prefToken", "");

        return CallSphincterAPI(url);
    }

    final String CallSphincterAPI(String urlstr) {
        try {
            URL url = new URL(urlstr);

            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setConnectTimeout(2000);

            return readStream(con.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    private String readStream(InputStream in) {
        BufferedReader reader = null;
        String result ="";
        try {
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";

            while ((line = reader.readLine()) != null) {
                result += line;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    protected void onPostExecute(String result) {
        listener.onTaskCompleted(result.trim());
    }
}




Java Source Code List

com.michiwend.spincterremote.ApplicationTest.java
com.michiwend.spincterremote.MainActivity.java
com.michiwend.spincterremote.OnTaskCompleted.java
com.michiwend.spincterremote.SettingsActivity.java
com.michiwend.spincterremote.SphincterRequestHandler.java