Android Open Source - Android-Apps J S O N Request Factory






From Project

Back to project page Android-Apps.

License

The source code is released under:

Apache License

If you think the Android project Android-Apps 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.kniezrec.xbmcgear.connection;
//from   w  w  w .jav  a  2  s .  c o  m
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

public final class JSONRequestFactory {
    public final static int REQ_PLAYER_PLAYPAUSE = 800;
    public final static int REQ_PLAYER_SEEK_FOR = 804;
    public final static int REQ_PLAYER_SEEK_BAC = 805;
    public final static int REQ_ACTIVE_PLAYERS = 812;
    public final static int REQ_GET_PROPERTIES = 813;
    public final static int REQ_GET_ITEMS = 815;
    public final static int REQ_VOLUME = 816;
    public final static int REQ_GET_ITEM = 818;
    /*
     * Request codes from GEAR
     */
    private final static int REQ_GOTO_PLAYLIST = 700;
    private final static int REQ_PLAYER_STOP = 801;
    private final static int REQ_PLAYER_NEXT = 802;
    private final static int REQ_PLAYER_PREV = 803;
    private final static int REQ_MOVE_UP = 806;
    private final static int REQ_MOVE_DOWN = 807;
    private final static int REQ_MOVE_LEFT = 808;
    private final static int REQ_MOVE_RIGHT = 809;
    private final static int REQ_SELECT = 810;
    public final static int REQ_SHUTDOWN = 811;
    private final static int REQ_BACK = 814;
    private final static int REQ_HOME = 817;
    private final static int REQ_CONTEXT_MENU = 819;
    public final static int REQ_GET_VERSION = 820;

    private static final String TAG = "RequestAction";
    /*
     * Hardcoded json requests - it's not necessary to create JSON dynamically because I need only
     * restricted set of options in GEAR
     */
    public final static String GET_MAC_ADDR = "{\"jsonrpc\": \"2.0\", \"method\": \"XBMC.GetInfoLabels\", \"params\": { \"labels\": [ \"Network.MacAddress\"] }, \"id\": 1}";
    private final static String PlayPause = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.PlayPause\",\"id\":1,\"params\":{\"playerid\":0}}";
    private final static String Stop = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.Stop\",\"id\":1,\"params\":{\"playerid\":0}}";
    private final static String GoToPos = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.GoTo\",\"id\":1,\"params\":{\"playerid\":0}}";
    private final static String GoToNext = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.GoTo\",\"id\":1,\"params\":{\"playerid\":0,\"to\":\"next\"}}";
    private final static String GoToPrev = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.GoTo\",\"id\":1,\"params\":{\"playerid\":0,\"to\":\"previous\"}}";
    private final static String SetSpeedInc = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.SetSpeed\",\"id\":1,\"params\":{\"playerid\":0,\"speed\":\"increment\"}}";
    private final static String SetSpeedDec = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.SetSpeed\",\"id\":1,\"params\":{\"playerid\":0,\"speed\":\"decrement\"}}";
    private final static String MoveUp = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.Up\", \"id\": 1}";
    private final static String MoveDown = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.Down\", \"id\": 1}";
    private final static String MoveLeft = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.Left\", \"id\": 1}";
    private final static String MoveRight = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.Right\", \"id\": 1}";
    private final static String Select = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.Select\", \"id\": 1}";
    private final static String ContextMenu = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.ContextMenu\", \"id\": 1}";
    private final static String Shutdown = "{\"jsonrpc\": \"2.0\", \"method\": \"System.Shutdown\", \"id\": 1}";
    private final static String ActivePlayers = "{\"jsonrpc\": \"2.0\", \"method\": \"Player.GetActivePlayers\", \"id\": 1}";
    private final static String GetProperties = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.GetProperties\",\"id\":1,\"params\":{\"playerid\":0,\"properties\":[\"playlistid\",\"speed\",\"position\",\"totaltime\",\"time\"]}}";
    private final static String Back = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.Back\", \"id\": 1}";
    private final static String GetItems = "{\"jsonrpc\":\"2.0\",\"method\":\"Playlist.GetItems\",\"id\":1,\"params\":{\"playlistid\":0,\"properties\":[\"title\",\"album\",\"artist\",\"duration\"]}}";
    private final static String GoHome = "{\"jsonrpc\": \"2.0\", \"method\": \"Input.Home\", \"id\": 1}";
    private final static String GetItem = "{\"jsonrpc\":\"2.0\",\"method\":\"Player.GetItem\",\"id\":1,\"params\":{\"playerid\":0,\"properties\":[\"title\",\"season\",\"episode\",\"plot\",\"runtime\",\"showtitle\"]}}";
    private final static String SetVolume = "{\"jsonrpc\":\"2.0\",\"method\":\"Application.SetVolume\",\"id\":1,\"params\":{}}";
    private final static String GetParams = "{\"jsonrpc\": \"2.0\", \"method\": \"Application.GetProperties\", \"params\": { \"properties\": [ \"name\", \"version\", \"volume\"] }, \"id\": 1}";

    public static JSONRPCRequest processQuery(int queryRequest) {
        JSONObject json = null;
        JSONRPCRequest request = new JSONRPCRequest();
        try {
            switch (queryRequest) {
                case REQ_HOME:
                    json = new JSONObject(GoHome);
                    break;
                case REQ_CONTEXT_MENU:
                    json = new JSONObject(ContextMenu);
                    break;
                case REQ_PLAYER_PLAYPAUSE:
                    json = new JSONObject(PlayPause);
                    break;
                case REQ_PLAYER_STOP:
                    json = new JSONObject(Stop);
                    break;
                case REQ_PLAYER_NEXT:
                    json = new JSONObject(GoToNext);
                    break;
                case REQ_PLAYER_PREV:
                    json = new JSONObject(GoToPrev);
                    break;
                case REQ_PLAYER_SEEK_FOR:
                    json = new JSONObject(SetSpeedInc);
                    break;
                case REQ_PLAYER_SEEK_BAC:
                    json = new JSONObject(SetSpeedDec);
                    break;
                case REQ_MOVE_UP:
                    json = new JSONObject(MoveUp);
                    break;
                case REQ_MOVE_DOWN:
                    json = new JSONObject(MoveDown);
                    break;
                case REQ_MOVE_LEFT:
                    json = new JSONObject(MoveLeft);
                    break;
                case REQ_MOVE_RIGHT:
                    json = new JSONObject(MoveRight);
                    break;
                case REQ_SELECT:
                    json = new JSONObject(Select);
                    break;
                case REQ_SHUTDOWN:
                    json = new JSONObject(Shutdown);
                    break;
                case REQ_BACK:
                    json = new JSONObject(Back);
                    break;
                case REQ_GET_VERSION:
                    json = new JSONObject(GetParams);
                    break;
                default:
                    json = new JSONObject();
                    break;
            }
        } catch (JSONException e) {
            Log.v("RequestAction", e.getMessage());
        }

        request.setMethod(queryRequest);
        request.setRequest(json);

        return request;
    }

    public static JSONObject getActivPlayers() throws JSONException {
        return new JSONObject(ActivePlayers);
    }

    public static JSONObject getProperties() throws JSONException {
        return new JSONObject(GetProperties);
    }

    public static JSONObject getItems() throws JSONException {
        return new JSONObject(GetItems);
    }

    public static JSONObject getItem() throws JSONException {
        return new JSONObject(GetItem);
    }

    public static JSONObject getVersion() throws JSONException {
        return new JSONObject(GetParams);
    }

    public static JSONRPCRequest goTo(int id) {
        JSONRPCRequest request = new JSONRPCRequest();
        request.setMethod(REQ_GOTO_PLAYLIST);
        JSONObject req = null;
        try {
            req = new JSONObject(GoToPos);
            JSONObject params = req.optJSONObject("params");
            if (params != null) {
                params.put("to", id);
            }
        } catch (JSONException e) {
            Log.v(TAG, e.getMessage());
        }
        request.setRequest(req);
        return request;
    }

    public static JSONRPCRequest setVolume(int level) {
        JSONRPCRequest request = new JSONRPCRequest();
        request.setMethod(REQ_VOLUME);
        JSONObject req = null;
        try {
            req = new JSONObject(SetVolume);
            JSONObject params = req.optJSONObject("params");
            if (params != null) {
                params.put("volume", level);
            }
        } catch (JSONException e) {
            Log.v(TAG, e.getMessage());
        }
        request.setRequest(req);
        return request;
    }

}




Java Source Code List

com.kniezrec.remoterecorder.Communication.java
com.kniezrec.remoterecorder.MainServiceConnection.java
com.kniezrec.remoterecorder.MainService.java
com.kniezrec.remoterecorder.RequestType.java
com.kniezrec.remoterecorder.Request.java
com.kniezrec.voiceremote2.BSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremote2.BSeriesSender.java
com.kniezrec.voiceremote2.CSeriesButtons.java
com.kniezrec.voiceremote2.CSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremote2.CSeriesSender.java
com.kniezrec.voiceremote2.CommandsFragment.java
com.kniezrec.voiceremote2.Commands.java
com.kniezrec.voiceremote2.Discovery.java
com.kniezrec.voiceremote2.FSeriesButtons.java
com.kniezrec.voiceremote2.Group.java
com.kniezrec.voiceremote2.HelpFragment.java
com.kniezrec.voiceremote2.HostnamePreference.java
com.kniezrec.voiceremote2.KeyCodeSender.java
com.kniezrec.voiceremote2.ListActionsFragment.java
com.kniezrec.voiceremote2.MainActivity.java
com.kniezrec.voiceremote2.MainFragment.java
com.kniezrec.voiceremote2.Mapper.java
com.kniezrec.voiceremote2.MyExpandableListAdapter.java
com.kniezrec.voiceremote2.NewActionEdit.java
com.kniezrec.voiceremote2.NewActionSingleEdit.java
com.kniezrec.voiceremote2.NewAction.java
com.kniezrec.voiceremote2.RemoteButton.java
com.kniezrec.voiceremote2.SenderFactory.java
com.kniezrec.voiceremote2.Sender.java
com.kniezrec.voiceremote2.SettingsActivity.java
com.kniezrec.voiceremote2.TextSender.java
com.kniezrec.voiceremotefree.BSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremotefree.BSeriesSender.java
com.kniezrec.voiceremotefree.CSeriesButtons.java
com.kniezrec.voiceremotefree.CSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremotefree.CSeriesSender.java
com.kniezrec.voiceremotefree.Commands.java
com.kniezrec.voiceremotefree.Discovery.java
com.kniezrec.voiceremotefree.FSeriesButtons.java
com.kniezrec.voiceremotefree.HelpActivity.java
com.kniezrec.voiceremotefree.HostnamePreference.java
com.kniezrec.voiceremotefree.KeyCodeSender.java
com.kniezrec.voiceremotefree.ListActionsActivity.java
com.kniezrec.voiceremotefree.MainActivity.java
com.kniezrec.voiceremotefree.Mapper.java
com.kniezrec.voiceremotefree.NewActionEdit.java
com.kniezrec.voiceremotefree.NewActionSingleEdit.java
com.kniezrec.voiceremotefree.NewAction.java
com.kniezrec.voiceremotefree.RemoteButton.java
com.kniezrec.voiceremotefree.SenderFactory.java
com.kniezrec.voiceremotefree.Sender.java
com.kniezrec.voiceremotefree.Setings.java
com.kniezrec.voiceremotefree.SettingsActivity.java
com.kniezrec.voiceremotefree.TextSender.java
com.kniezrec.xbmcgear.connection.AndroidApplication.java
com.kniezrec.xbmcgear.connection.Connection.java
com.kniezrec.xbmcgear.connection.GearJSON.java
com.kniezrec.xbmcgear.connection.JSONRPCRequest.java
com.kniezrec.xbmcgear.connection.JSONRequestFactory.java
com.kniezrec.xbmcgear.connection.NSDResolve.java
com.kniezrec.xbmcgear.connection.NSDSearch.java
com.kniezrec.xbmcgear.connection.ProviderConnection.java
com.kniezrec.xbmcgear.connection.ProviderService.java
com.kniezrec.xbmcgear.connection.ResponseParser.java
com.kniezrec.xbmcgear.connection.WakeOnLan.java
com.kniezrec.xbmcgear.player.Kodi.java
com.kniezrec.xbmcgear.player.Player.java
com.kniezrec.xbmcgear.player.Playlist.java
com.kniezrec.xbmcgear.player.Song.java
com.kniezrec.xbmcgear.player.Video.java
com.kniezrec.xbmcgear.preferences.HostTable.java
com.kniezrec.xbmcgear.preferences.Host.java
com.kniezrec.xbmcgear.preferences.HostsDataSource.java
com.kniezrec.xbmcgear.preferences.HostsDatabaseHelper.java
com.kniezrec.xbmcgear.preferences.SharedPreferencesUtil.java
com.kniezrec.xbmcgear.presentation.AnimationManager.java
com.kniezrec.xbmcgear.presentation.AutoConfigurationActivity.java
com.kniezrec.xbmcgear.presentation.HostSetActivity.java
com.kniezrec.xbmcgear.presentation.InstanceActivity.java
com.kniezrec.xbmcgear.presentation.MainActivity.java
com.kniezrec.xbmcgear.presentation.StyleDialogFragment.java
com.kniezrec.xbmcgear.presentation.ViewMode.java
com.uraroji.garage.android.lame.SimpleLame.java
com.uraroji.garage.android.mp3recvoice.RecMicToMp3.java
de.quist.samy.remocon.Base64.java
de.quist.samy.remocon.Base64.java
de.quist.samy.remocon.ConnectionDeniedException.java
de.quist.samy.remocon.ConnectionDeniedException.java
de.quist.samy.remocon.Key.java
de.quist.samy.remocon.Key.java
de.quist.samy.remocon.Loggable.java
de.quist.samy.remocon.Loggable.java
de.quist.samy.remocon.RemoteReader.java
de.quist.samy.remocon.RemoteReader.java
de.quist.samy.remocon.RemoteSession.java
de.quist.samy.remocon.RemoteSession.java