Esempio n. 1
0
    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    protected Void doInBackground(String... params) {

      String userName = params[0];
      String userScore = params[1];
      String hostName = "52.26.125.237";
      Integer portNumber = 4444;
      JSONOutputStream outToServer;

      HashMap<String, String> highScore = new HashMap<>();

      highScore.put("Command", "Record High Score");
      highScore.put("Username", userName);
      highScore.put("Score", userScore);

      try (Socket toServer = new Socket(hostName, portNumber)) {

        outToServer = new JSONOutputStream(toServer.getOutputStream());
        outToServer.writeObject(highScore);

      } catch (IOException | JSONException e) {

        Toast.makeText(
                getApplicationContext(),
                "Couldn't connect to server."
                    + "Please ensure that either Wi-Fi or Data is enabled.",
                Toast.LENGTH_SHORT)
            .show();
      }

      return null;
    }
Esempio n. 2
0
    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    protected Void doInBackground(Void... params) {

      String hostName = "52.26.125.237";
      Integer portNumber = 4444;
      JSONOutputStream outToServer;
      JSONInputStream inFromServer;

      HashMap<String, String> highScore = new HashMap<>();

      highScore.put("Command", "View High Scores");
      Socket toServer = null;

      try {

        toServer = new Socket(hostName, portNumber);
        outToServer = new JSONOutputStream(toServer.getOutputStream());
        outToServer.writeObject(highScore);

      } catch (IOException | JSONException e) {

        Toast.makeText(
                getApplicationContext(),
                "Couldn't connect to server."
                    + "Please ensure that either Wi-Fi or Data is enabled.",
                Toast.LENGTH_SHORT)
            .show();
      }

      try {
        inFromServer = new JSONInputStream(toServer.getInputStream());
        highScores = (ArrayList<String>) inFromServer.readObject();

      } catch (IOException | JSONException e) {

        e.printStackTrace();
      }

      highScoresList = highScores.toArray(new CharSequence[highScores.size()]);

      return null;
    }