Android Open Source - Werewolf-Android-Client Get Kills






From Project

Back to project page Werewolf-Android-Client.

License

The source code is released under:

Copyright (c) 2013, Timothy Cohen All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: ...

If you think the Android project Werewolf-Android-Client 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 edu.wm.werewolf_client;
//from   w w  w . j av a2  s. c  o m
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpResponse;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class GetKills {
  
  String TAG = "GetKills";
  static JSONObject jObj = null;

  
  public  int getKills(String username, String password) {
    try{
      Log.i(TAG, "Username is: "+username);
      Log.i(TAG, "Password is: "+password);


      HttpClient client = new DefaultHttpClient();
      URI website = new URI("http://powerful-depths-2851.herokuapp.com/players/kills?username="+username);
      HttpGet request = new HttpGet();
      request.setURI(website);
      request.addHeader(BasicScheme.authenticate(
          new UsernamePasswordCredentials(username, password),
          "UTF-8", false));
      HttpResponse response = client.execute(request);
      response.getStatusLine().getStatusCode();

      BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
      StringBuffer sb = new StringBuffer("");
      String l = "";
      String nl = System.getProperty("line.separator");
      while ((l = in.readLine()) !=null){
        sb.append(l + nl);
      }
      
      Log.i(TAG, "response from server was: "+sb);

      // try parse the string to a JSON object
      try {
        jObj = new JSONObject(sb.toString());
      } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
      }
      Log.i(TAG, "jObj is: "+jObj);
      int kills = jObj.getInt("kills");
      Log.i(TAG, "number of kills: "+kills);
      in.close();
      
      return kills;


    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (URISyntaxException e) {
      e.printStackTrace();
    } catch (JSONException e) {
      e.printStackTrace();
    } 
    
    Log.e(TAG, "Could not get number of kills, asumming 0!");
    return 0;//Placeholder if we can't actually get the value


  }

}




Java Source Code List

edu.wm.werewolf_client.DayTab.java
edu.wm.werewolf_client.FindLocation.java
edu.wm.werewolf_client.GetAllAlive.java
edu.wm.werewolf_client.GetKills.java
edu.wm.werewolf_client.IsNight.java
edu.wm.werewolf_client.KillAttempt.java
edu.wm.werewolf_client.Locate.java
edu.wm.werewolf_client.MainActivity.java
edu.wm.werewolf_client.MainInterface.java
edu.wm.werewolf_client.NightTab.java
edu.wm.werewolf_client.ObatainLocation.java
edu.wm.werewolf_client.Play.java
edu.wm.werewolf_client.Player.java
edu.wm.werewolf_client.Register.java
edu.wm.werewolf_client.Stats.java
edu.wm.werewolf_client.UserTab.java
edu.wm.werewolf_client.UsernameAndPassword.java
edu.wm.werewolf_client.Validate.java
edu.wm.werewolf_client.Vote.java
edu.wm.werewolf_client.isWerewolf.java