Android Open Source - Android-Apps Response Parser






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 . java  2 s. c o m
import com.kniezrec.xbmcgear.player.Kodi;
import com.kniezrec.xbmcgear.player.Player;
import com.kniezrec.xbmcgear.player.Song;

import org.json.JSONArray;
import org.json.JSONObject;

class ResponseParser {
    public static void parse(Player player, JSONRPCRequest res) {
        if (player != null && res != null && res.getResponse() != null) {
            JSONObject rJSON = res.getResponse();
            JSONObject result = rJSON.optJSONObject("result");
            switch (res.getMethod()) {
                case JSONRequestFactory.REQ_PLAYER_PLAYPAUSE:
                    if (result != null) {
                        player.setSpeed(result.optInt("speed"));
                        if (player.getSpeed() == 0) {
                            ProviderService.getInstance().sendToGear(GearJSON.PAUSE);
                        } else if (player.getSpeed() == 1) {
                            ProviderService.getInstance().sendToGear(GearJSON.PLAY);
                        }
                    }
                case JSONRequestFactory.REQ_PLAYER_SEEK_BAC:
                case JSONRequestFactory.REQ_PLAYER_SEEK_FOR:
                    if (result != null) {
                        player.setSpeed(result.optInt("speed"));
                        if (player.getSpeed() == 1) {
                            ProviderService.getInstance().sendToGear(GearJSON.PLAY);
                        } else {
                            ProviderService.getInstance().sendToGear(GearJSON.PAUSE);
                        }
                    }
                    break;
                case JSONRequestFactory.REQ_ACTIVE_PLAYERS:
                    JSONArray resultArr = rJSON.optJSONArray("result");
                    if (resultArr != null && resultArr.length() > 0) {
                        JSONObject resObj;
                        resObj = resultArr.optJSONObject(0);
                        if (resObj != null) {
                            player.setId(resObj.optInt("playerid"));
                            player.setType(resObj.optString("type"));
                        }
                    } else {
                        ProviderService.getInstance().sendToGear(GearJSON.STOP);
                        player.setPosition(-1);
                    }
                    break;
                case JSONRequestFactory.REQ_GET_PROPERTIES:
                    if (result != null) {
                        player.playlist.setId(result.optInt("playlistid"));
                        int current = result.optInt("position");
                        int speed = result.optInt("speed");
                        if (speed != player.getSpeed()) {
                            if (speed == 0) {
                                ProviderService.getInstance().sendToGear(
                                        GearJSON.PAUSE);
                            } else if (speed == 1) {
                                ProviderService.getInstance().sendToGear(
                                        GearJSON.PLAY);
                            }
                        }
                        player.setSpeed(speed);
                        if (current != player.getPosition()
                                && !player.playlist.isListEmpty()) {
                            player.setPosition(current);
                            player.playlist.setCurrent(current);
                            ProviderService.getInstance().sendToGear(
                                    player.playlist.getJsonSong(current));
                            if (player.getSpeed() > 0) {
                                ProviderService.getInstance().sendToGear(
                                        GearJSON.PLAY);
                            }
                        }

                        JSONObject time = result.optJSONObject("time");
                        if (time != null) {
                            player.setCurH(time.optInt("hours"));
                            player.setCurMin(time.optInt("minutes"));
                            player.setCurSec(time.optInt("seconds"));
                        }
                        JSONObject total = result
                                .optJSONObject("totaltime");
                        if (total != null
                                && player.isNewTotalTime(total
                                .hashCode())) {
                            player.setTotalH(total.optInt("hours"));
                            player.setTotalMin(total.optInt("minutes"));
                            player.setTotalSec(total.optInt("seconds"));
                            player.setTotalTimeHash(total.hashCode());
                        }
                    }
                    break;
                case JSONRequestFactory.REQ_GET_VERSION:
                    if (result != null) {
                        player.kodi = new Kodi();
                        String name = result.optString("name");
                        JSONObject ver = result.optJSONObject("version");
                        int major = ver.optInt("major");
                        int minor = ver.optInt("minor");
                        player.kodi.setMajor(major);
                        player.kodi.setMinor(minor);
                        player.kodi.setName(name);
                        int volume = result.optInt("volume");
                        String volLevel = String.format(GearJSON.VOLUME, volume);
                        ProviderService.getInstance().sendToGear(volLevel);
                    }
                    break;
                case JSONRequestFactory.REQ_VOLUME:
                    int value = rJSON.optInt("result");
                    String volLevel = String.format(GearJSON.VOLUME, value);
                    ProviderService.getInstance().sendToGear(volLevel);
                    break;
                case JSONRequestFactory.REQ_GET_ITEM:
                    if (result != null) {
                        JSONObject item = result.optJSONObject("item");
                        if (item != null) {
                            String title = item.optString("title");
                            String showtitle = item.optString("showtitle");
                            if (player.videoPlayer.isNewVideo(title
                                    .hashCode())) {
                                player.videoPlayer.setTitle(title);
                                player.videoPlayer.setShowTitle(showtitle);
                                ProviderService.getInstance().sendToGear(
                                        player.videoPlayer
                                                .getVideoTitleJSON());
                            }
                        }
                    }
                    break;
                case JSONRequestFactory.REQ_GET_ITEMS:
                    if (result != null) {
                        JSONObject limits = result.optJSONObject("limits");
                        if (limits != null
                                && player.playlist.areNewLimits(limits
                                .hashCode())) {
                            player.playlist
                                    .setTotal(limits.optInt("total"));
                            player.playlist
                                    .setStart(limits.optInt("start"));
                            player.playlist.setEnd(limits.optInt("end"));
                            player.playlist
                                    .setLimitsHash(limits.hashCode());
                        }
                        JSONArray items = result.optJSONArray("items");
                        JSONObject it;
                        if (items != null
                                && player.playlist.areNewItems(items
                                .toString().hashCode())) {
                            player.playlist.clearSongs();
                            player.playlist.setItemsHash(items.toString()
                                    .hashCode());
                            for (int i = 0; i < items.length(); i++) {
                                it = items.optJSONObject(i);
                                if (it != null) {
                                    Song s = new Song();
                                    s.setLabel(it.optString("label"));
                                    s.setTitle(it.optString("title"));
                                    JSONArray ar = it
                                            .optJSONArray("artist");
                                    if (ar != null) {
                                        s.setArtist(ar.optString(0));
                                        player.playlist.addSong(s);
                                    }
                                }
                            }
                            ProviderService.getInstance().sendToGear(
                                    player.playlist.toString());
                            int pos = player.getPosition();
                            if (pos != -1) {
                                ProviderService.getInstance().sendToGear(
                                        player.playlist.getJsonSong(pos));
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }
}




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