Android Open Source - NAQT-Scoresheet-for-Android Data Export

From Project

Back to project page NAQT-Scoresheet-for-Android.


The source code is released under:

Apache License

If you think the Android project NAQT-Scoresheet-for-Android 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.naqtscoresheet;
//from  w w w . java 2 s .c  o  m
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.widget.Toast;

public class DataExport {
  public enum Format {
  public static void postGameData(Context context, Game game, String url, Format format) {
    String gameData;
    if (format == Format.JSON) {
      System.err.println("DEBUG: generating JSON...");
      Visitor visitor = new JSONVisitor();
      gameData = visitor.toString();
    else if (format == Format.XML) {
      System.err.println("DEBUG: generating XML...");
      Visitor visitor = new XMLVisitor();
      gameData = visitor.toString();
    else {
      throw new RuntimeException("Unknown data type.");
      HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost(url);

      try {
          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
          nameValuePairs.add(new BasicNameValuePair("game_data", gameData));
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

          ResponseHandler<String> handler = new BasicResponseHandler();
          String responseBody = httpclient.execute(httppost, handler);
          Toast t = Toast.makeText(context, "Game upload successful.", Toast.LENGTH_SHORT);
          if (responseBody != null) {
          else {
            System.err.println("Response body was null.");
    } catch (HttpResponseException e) {
        System.err.println("DEBUG: POST failed: HttpResponseException.");
      } catch (ClientProtocolException e) {
        System.err.println("DEBUG: POST failed: ClientProtocolException.");
      } catch (IOException e) {
        System.err.println("DEBUG: POST failed: IOException.");
  private static void displayPostFailureError(Context context) {
    Toast t = Toast.makeText(context, "Failed to upload game.", Toast.LENGTH_SHORT);;

Java Source Code List