net.noviden.android.shoutout.NewPostScreen.java Source code

Java tutorial

Introduction

Here is the source code for net.noviden.android.shoutout.NewPostScreen.java

Source

/**
 ShoutOut : an app for anonymous broadcasts
 Copyright (C) 2014 Tristan Kernan
    
 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 net.noviden.android.shoutout;

import android.app.ActionBar;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

public class NewPostScreen extends Activity {

    private EditText mNewPost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_post_screen);

        mNewPost = (EditText) findViewById(R.id.send_text_field);

        ActionBar mNewPostActionBar = getActionBar();
        mNewPostActionBar.setDisplayHomeAsUpEnabled(true);
        mNewPostActionBar.setHomeButtonEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.new_post_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
        case R.id.action_send_post:
            sendPost(mNewPost.getText().toString());
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public void sendPost(String newPost) {
        // check length of post
        if (newPost.length() > 200) {
            return;
        }

        SendPostAsyncTask mSendPostTask = new SendPostAsyncTask();
        mSendPostTask.execute(newPost);

    }

    public class SendPostAsyncTask extends AsyncTask<String, Void, Void> {
        @Override
        protected Void doInBackground(String... params) {
            if (params.length == 1) {
                String myArg = params[0];

                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    URI myURI = new URI("http", null, "INSERT IP ADDRESS", 3000, "/api/addPost", null, null);
                    HttpPost httppost = new HttpPost(myURI);

                    // Request parameters and other properties.
                    List<NameValuePair> postParams = new ArrayList<NameValuePair>(2);
                    postParams.add(new BasicNameValuePair("post", myArg));
                    httppost.setEntity(new UrlEncodedFormEntity(postParams, "UTF-8"));

                    //Execute and get the response.
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    if (entity != null) {
                        InputStream instream = entity.getContent();
                        try {
                            // do something useful

                        } finally {
                            instream.close();

                            return null;
                        }
                    }
                } catch (Exception e) {
                    // bad i guess
                    Log.d("interwebs", "add new message failed!");
                }

            }

            return null;
        }
    }

}