Android Open Source - OD-PlayerStats Parser






From Project

Back to project page OD-PlayerStats.

License

The source code is released under:

MIT License

If you think the Android project OD-PlayerStats 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 net.black_pixel.od_playerstats.parser;
/*w  ww .j  av  a  2  s  .c  om*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class Parser {
  private ArrayList<String> parsedValues = null;
  
  public ArrayList<String> getStatsByID(String urlString) {
    URL statsUrl = null;
    try {
      statsUrl = new URL(urlString);
    } catch (MalformedURLException e1) {
      e1.printStackTrace();
      return null;
    }
    
    InputStream content = null;
    try {
      content = (InputStream) statsUrl.getContent();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return null;
    }
    
    BufferedReader reader = new BufferedReader(new InputStreamReader(
        content));
    StringBuilder out = new StringBuilder();
    String line;
    try {
      while ((line = reader.readLine()) != null) {
        out.append(line);
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
    return parseStats(out.toString());
  }

  private ArrayList<String> parseStats(String rawStats) {
    Pattern pattern = Pattern.compile(".*name=(.*)"
        + "&points=(.*)&titel.*" + "warpoints=(.*)&gesinnung.*"
        + "allianz_name=(.*)&allianz_punkte.*"
        + "plcount=(.*)&kriegs.*" + "questpunkte=(.*)&"
        + "handelspunkte=(.*)&rasse" + "=(.*)&allianzmembers.*");
    Matcher matcher = pattern.matcher(rawStats);
    if (matcher.find()) {
      if (matcher.group(1).length() < 2) { // if no user was found
        return null;
      }
      parsedValues = new ArrayList<String>(); // not pretty, just clean it
      parsedValues.add(matcher.group(1));
      parsedValues.add(matcher.group(2));
      parsedValues.add(matcher.group(3));
      parsedValues.add(matcher.group(4));
      parsedValues.add(matcher.group(5));
      parsedValues.add(matcher.group(6));
      parsedValues.add(matcher.group(7));
      parsedValues.add(matcher.group(8));
    } else {
      Log.i("Error", "Invalid data.");
      return null;
    }
    return parsedValues;
  }
  
  public ArrayList<String> getCurrentValues() {
    return parsedValues;
  }
  
  public void setValues(ArrayList<String> parsedValues) {
    this.parsedValues = parsedValues;
  }
}




Java Source Code List

net.black_pixel.od_playerstats.MainActivity.java
net.black_pixel.od_playerstats.fileIO.FileIO.java
net.black_pixel.od_playerstats.parser.Parser.java