Java tutorial
/* * Copyright (C) 2013 - Gareth Llewellyn * * This file is part of the Unofficial HackedIO Android App https://github.com/NetworksAreMadeOfString/HackedIO-Android-App * * This app is unofficial. The "Hacked.io" logo is copyright (and maybe even a trademark of) Telefnica UK Limited. * The author of this app is not affiliated with Telefnica UK Limited, The Lab ( https://thelab.o2.com/ ) or * Geeks of London ( http://geeksoflondon.com/ ) although the author <strong>is</strong> an attendee. * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/> */ package app.hacked; import android.content.Intent; import android.graphics.Typeface; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.Volley; import org.json.JSONObject; /** * Created by Gareth on 23/06/13. */ public class AddProjectFragment extends Fragment { RequestQueue queue = null; EditText ProjectNameET = null; EditText SynopsisET = null; EditText DescET = null; EditText TeamMemberET = null; EditText TechET = null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.add_project_fragment, null); ((TextView) view.findViewById(R.id.Title)).setTypeface(Typeface.create("sans-serif-thin", Typeface.NORMAL)); ProjectNameET = (EditText) view.findViewById(R.id.ProjectNameET); SynopsisET = (EditText) view.findViewById(R.id.SynopsisET); DescET = (EditText) view.findViewById(R.id.DescET); TeamMemberET = (EditText) view.findViewById(R.id.TeamMemberET); TechET = (EditText) view.findViewById(R.id.ProjectNameET); ((Button) view.findViewById(R.id.CancelButton)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { getActivity().finish(); } }); ((Button) view.findViewById(R.id.addProjectButton)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (ProjectNameET.getText().toString().equals("")) { Toast.makeText(getActivity(), "A project needs a name for people to recognise!", Toast.LENGTH_SHORT).show(); return; } JSONObject post = new JSONObject(); try { post.put("project_name", ProjectNameET.getText().toString()); post.put("project_synopsis", SynopsisET.getText().toString()); post.put("project_desc", DescET.getText().toString()); post.put("project_team", TeamMemberET.getText().toString()); post.put("project_tech", TechET.getText().toString()); post.put("auth", API.md5(API.BETTER_THAN_NOTHING_STUFF_TO_PREVENT_INJECTION_ATTEMPTS + ProjectNameET.getText().toString())); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getActivity(), "An Error Was encountered parsing the entered details: " + e.getMessage(), Toast.LENGTH_SHORT).show(); return; } JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, "http://hackedioapp.networksaremadeofstring.co.uk/addproject.php", post, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.e("response", response.toString()); try { if (response.has("success") && response.getBoolean("success")) { Toast.makeText(getActivity(), "Project added successfully!", Toast.LENGTH_SHORT).show(); if (getActivity().findViewById(R.id.ProjectDetails) != null) { Bundle arguments = new Bundle(); arguments.putInt(ProjectDetailsFragment.PROJECTID, response.getInt("uuid")); arguments.putString(ProjectDetailsFragment.NAME, ProjectNameET.getText().toString()); arguments.putString(ProjectDetailsFragment.SYNOPSIS, SynopsisET.getText().toString()); arguments.putString(ProjectDetailsFragment.DESCRIPTION, DescET.getText().toString()); arguments.putString(ProjectDetailsFragment.TECHNOLOGIES, TechET.getText().toString()); arguments.putInt(ProjectDetailsFragment.POPULARITY, 1); arguments.putString(ProjectDetailsFragment.TEAMMEMBERS, TeamMemberET.getText().toString()); arguments.putBoolean(ProjectDetailsFragment.ARG_2PANE, true); ProjectDetailsFragment fragment = new ProjectDetailsFragment(); fragment.setArguments(arguments); fragment.setHasOptionsMenu(true); getActivity().getSupportFragmentManager().beginTransaction() .replace(R.id.ProjectDetails, fragment).commit(); } else { getActivity().finish(); Intent detailIntent = new Intent(getActivity(), ProjectDetailsActivity.class); detailIntent.putExtra(ProjectDetailsFragment.PROJECTID, response.getInt("uuid")); detailIntent.putExtra(ProjectDetailsFragment.NAME, ProjectNameET.getText().toString()); detailIntent.putExtra(ProjectDetailsFragment.SYNOPSIS, SynopsisET.getText().toString()); detailIntent.putExtra(ProjectDetailsFragment.DESCRIPTION, DescET.getText().toString()); detailIntent.putExtra(ProjectDetailsFragment.TECHNOLOGIES, TechET.getText().toString()); detailIntent.putExtra(ProjectDetailsFragment.POPULARITY, 1); detailIntent.putExtra(ProjectDetailsFragment.TEAMMEMBERS, TeamMemberET.getText().toString()); detailIntent.putExtra(ProjectDetailsFragment.ARG_2PANE, true); startActivity(detailIntent); } } else { String error = ""; if (response.has("error")) error = ": " + response.getString("error"); Toast.makeText(getActivity(), "An Error Was encountered" + error, Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(getActivity(), "An Error Was encountered: " + e.getMessage(), Toast.LENGTH_SHORT).show(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // TODO Auto-generated method stub Toast.makeText(getActivity(), "An Error Was encountered: " + error.getMessage(), Toast.LENGTH_SHORT).show(); } }); queue.add(jsObjRequest); } }); return view; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); queue = Volley.newRequestQueue(getActivity()); } }