Skip to content

Instantly share code, notes, and snippets.

@kennyung6
Forked from sourabh86/ContactActivity.java
Created August 29, 2016 14:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kennyung6/39d3f956ebe350111519ab06a8c7eab8 to your computer and use it in GitHub Desktop.
Save kennyung6/39d3f956ebe350111519ab06a8c7eab8 to your computer and use it in GitHub Desktop.
Code to send data to Google Docs sheet from Android activity
package in.codesmith.contactusexample;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class ContactActivity extends ActionBarActivity {
public static final MediaType FORM_DATA_TYPE
= MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
//URL derived from form URL
public static final String URL="https://docs.google.com/forms/d/1dcq0Fgt5dSUoIiH119K5-eF-XMGiq3eVPt97Aocspx4/formResponse";
//input element ids found from the live form page
public static final String EMAIL_KEY="entry_313227136";
public static final String SUBJECT_KEY="entry_1834657755";
public static final String MESSAGE_KEY="entry_1110317093";
private final Context context;
private EditText emailEditText;
private EditText subjectEditText;
private EditText messageEditText;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
//save the activity in a context variable to be used afterwards
context =this;
//Get references to UI elements in the layout
Button sendButton = (Button)findViewById(R.id.sendButton);
emailEditText = (EditText)findViewById(R.id.emailEditText);
subjectEditText = (EditText)findViewById(R.id.subjectEditText);
messageEditText = (EditText)findViewById(R.id.messageEditText);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Make sure all the fields are filled with values
if(TextUtils.isEmpty(emailEditText.getText().toString()) ||
TextUtils.isEmpty(subjectEditText.getText().toString()) ||
TextUtils.isEmpty(messageEditText.getText().toString()))
{
Toast.makeText(context,"All fields are mandatory.",Toast.LENGTH_LONG).show();
return;
}
//Check if a valid email is entered
if(!android.util.Patterns.EMAIL_ADDRESS.matcher(emailEditText.getText().toString()).matches())
{
Toast.makeText(context,"Please enter a valid email.",Toast.LENGTH_LONG).show();
return;
}
//Create an object for PostDataTask AsyncTask
PostDataTask postDataTask = new PostDataTask();
//execute asynctask
postDataTask.execute(URL,emailEditText.getText().toString(),
subjectEditText.getText().toString(),
messageEditText.getText().toString());
}
});
}
//AsyncTask to send data as a http POST request
private class PostDataTask extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... contactData) {
Boolean result = true;
String url = contactData[0];
String email = contactData[1];
String subject = contactData[2];
String message = contactData[3];
String postBody="";
try {
//all values must be URL encoded to make sure that special characters like & | ",etc.
//do not cause problems
postBody = EMAIL_KEY+"=" + URLEncoder.encode(email,"UTF-8") +
"&" + SUBJECT_KEY + "=" + URLEncoder.encode(subject,"UTF-8") +
"&" + MESSAGE_KEY + "=" + URLEncoder.encode(message,"UTF-8");
} catch (UnsupportedEncodingException ex) {
result=false;
}
/*
//If you want to use HttpRequest class from http://stackoverflow.com/a/2253280/1261816
try {
HttpRequest httpRequest = new HttpRequest();
httpRequest.sendPost(url, postBody);
}catch (Exception exception){
result = false;
}
*/
try{
//Create OkHttpClient for sending request
OkHttpClient client = new OkHttpClient();
//Create the request body with the help of Media Type
RequestBody body = RequestBody.create(FORM_DATA_TYPE, postBody);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
//Send the request
Response response = client.newCall(request).execute();
}catch (IOException exception){
result=false;
}
return result;
}
@Override
protected void onPostExecute(Boolean result){
//Print Success or failure message accordingly
Toast.makeText(context,result?"Message successfully sent!":"There was some error in sending message. Please try again after some time.",Toast.LENGTH_LONG).show();
}
}
}
@cavin-kim
Copy link

Great, it has helped me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment