Android Open Source - Icinga-Mobile Net Controller






From Project

Back to project page Icinga-Mobile.

License

The source code is released under:

GNU General Public License

If you think the Android project Icinga-Mobile 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 mhst.dreamteam.IcingaClient.Controller;
/*from  ww  w  .jav a2 s  . c  om*/
import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import mhst.dreamteam.IcingaClient.GlobalConst;

/**
 * Network controller<br />
 * Uses to control network communication such as send request to server and get response data, etc.
 *
 * @author MinhNN
 */
public class NetController {
    /**
     * Uses to send request to a specific server via POST or GET method.
     *
     * @param sMethod "POST" - using POST method or "GET" - using GET method
     * @param sURL    Target URL to send request
     * @param sParam  (POST method only) data to send to server
     * @return a hashmap of response data<br />
     * "Code" - response code<br />
     * "Data" - data response from server (eg. json)<br />
     * "Error" - any error detected while communicating with server
     */
    public static Map<String, Object> sendRequest(String sMethod, String sURL, String sParam, Map<String, String> mProperty) {
        HttpURLConnection connection = null;
        Map<String, Object> result = new HashMap<String, Object>(); // Map data response from server
        try {
            // Create new URL
            URL HttpUrl = new URL(sURL);
            // Open connection for that URL
            connection = (HttpURLConnection) HttpUrl.openConnection();
            // Set properties for this connection
            connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
            // if mProperty != null, set list of property in mProperty
            if (mProperty != null) {
                String[] keyList = mProperty.keySet().toArray(new String[mProperty.size()]); // List of property name
                for (String keyName : keyList) {
                    connection.setRequestProperty(keyName, mProperty.get(keyName));
                }
            }
            connection.setDoInput(true);
            // Enable send request body (POST Method if true; otherwise, GET Method)
            connection.setDoOutput(sMethod.equalsIgnoreCase("POST"));

            if (sMethod.equalsIgnoreCase("POST")) {
                // Send request
                DataOutputStream post = new DataOutputStream(connection.getOutputStream());
                post.writeBytes(sParam);
                post.flush();
                post.close();
            }

            // Get response
            int sttCode = connection.getResponseCode(); // Get status code
            result.put("Code", sttCode);
            Log.i("NetController", "Response code = " + sttCode);

            // Get headers
            Map<String, Object> headers = new HashMap<String, Object>();
            String sHeader;
            int i = 1;
            while ((sHeader = connection.getHeaderFieldKey(i)) != null) {
                if (headers.containsKey(sHeader)) {
                    ArrayList<String> listValue = new ArrayList<String>();
                    listValue.add((String) headers.get(sHeader));
                    listValue.add(connection.getHeaderField(i));
                    headers.put(sHeader, listValue);
                } else {
                    headers.put(sHeader, connection.getHeaderField(i));
                }
                i++;
            }
            result.put("Headers", headers);

            // Get response data
            if (sttCode != 200) { // if status code != HTTP 200 OK, read response data from error stream
                BufferedReader errorReader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                String sErrorBuffer;
                StringBuilder sError = new StringBuilder();
                while ((sErrorBuffer = errorReader.readLine()) != null) {
                    sError.append(sErrorBuffer);
                    sError.append("\r\n");
                }
                errorReader.close();

                // Put data response message to map
                Log.e("NetController", sError.toString());
                result.put("Data", sError.toString());
            } else { // if status code is HTTP 200 OK, read data response from input stream
                BufferedReader responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String sBuffer;
                StringBuilder sResponse = new StringBuilder();
                while ((sBuffer = responseReader.readLine()) != null) {
                    sResponse.append(sBuffer);
                    sResponse.append("\r\n");
                }
                responseReader.close();

                // Put data response message to map
                Log.i("NetController", sResponse.toString());
                result.put("Data", sResponse.toString());
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
            result.put("Error", GlobalConst.ERROR_UNKNOWN_HOST);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            result.put("Error", GlobalConst.ERROR_UNKNOWN_ERROR);
        } catch (IOException e) {
            e.printStackTrace();
            result.put("Error", GlobalConst.ERROR_CONNECTION_ERROR);
        } finally {
            if (connection != null) connection.disconnect();
        }
        return result;
    }

}




Java Source Code List

mhst.dreamteam.ApplicationContext.java
mhst.dreamteam.ApplicationTest.java
mhst.dreamteam.ApplicationTest.java
mhst.dreamteam.MainActivity.java
mhst.dreamteam.IcingaClient.GlobalConfig.java
mhst.dreamteam.IcingaClient.GlobalConst.java
mhst.dreamteam.IcingaClient.Controller.NetControllerTest.java
mhst.dreamteam.IcingaClient.Controller.NetController.java
mhst.dreamteam.IcingaClient.Icinga.IcingaApiConst.java
mhst.dreamteam.IcingaClient.Icinga.IcingaApi.java
mhst.dreamteam.IcingaClient.Icinga.IcingaConst.java
mhst.dreamteam.IcingaClient.Icinga.IcingaExecutor.java
mhst.dreamteam.IcingaClient.Icinga.IcingaParam.java
mhst.dreamteam.IcingaClient.Icinga.IcingaUdt.java
mhst.dreamteam.IcingaClient.Icinga.package-info.java
mhst.dreamteam.IcingaClient.Interface.OnCompleteListener.java
mhst.dreamteam.IcingaClient.Interface.OnPieChartClickListener.java
mhst.dreamteam.IcingaClient.Json.JsonHelperTest.java
mhst.dreamteam.IcingaClient.Json.JsonHelper.java
mhst.dreamteam.IcingaClient.Misc.CookieMng.java
mhst.dreamteam.IcingaClient.Misc.CookieTest.java
mhst.dreamteam.IcingaClient.SessionMng.LogInTest.java
mhst.dreamteam.IcingaClient.SessionMng.Login.java
mhst.dreamteam.IcingaClient.SessionMng.Logout.java
mhst.dreamteam.IcingaClient.SessionMng.Session.java
mhst.dreamteam.IcingaService.ApplicationContext.java
mhst.dreamteam.IcingaService.DataUpdater.java
mhst.dreamteam.IcingaService.MessageReveicer.java
mhst.dreamteam.IcingaService.NotiBuilder.java
mhst.dreamteam.IcingaService.SQLHelper.java
mhst.dreamteam.IcingaService.SessionProvider.java
mhst.dreamteam.UI.Color.java
mhst.dreamteam.UI.GradientLine.java
mhst.dreamteam.UI.HostDetailsFragment.java
mhst.dreamteam.UI.HostlistAdapter.java
mhst.dreamteam.UI.HostlistFragment.java
mhst.dreamteam.UI.LoginActivity.java
mhst.dreamteam.UI.OverviewFragment.java
mhst.dreamteam.UI.PieGraph.java
mhst.dreamteam.UI.ProgressDialog.java
mhst.dreamteam.UI.ServiceDetailsFragment.java
mhst.dreamteam.UI.ServicelistAdapter.java
mhst.dreamteam.UI.ServicelistFragment.java
org.json.CDL.java
org.json.CookieList.java
org.json.Cookie.java
org.json.HTTPTokener.java
org.json.HTTP.java
org.json.JSONArray.java
org.json.JSONException.java
org.json.JSONML.java
org.json.JSONObject.java
org.json.JSONString.java
org.json.JSONStringer.java
org.json.JSONTokener.java
org.json.JSONWriter.java
org.json.Kim.java
org.json.Property.java
org.json.XMLTokener.java
org.json.XML.java
org.json.zip.BitInputStream.java
org.json.zip.BitOutputStream.java
org.json.zip.BitReader.java
org.json.zip.BitWriter.java
org.json.zip.Huff.java
org.json.zip.JSONzip.java
org.json.zip.Keep.java
org.json.zip.None.java
org.json.zip.PostMortem.java
org.json.zip.Unzipper.java
org.json.zip.Zipper.java