How to get data from a google spreadsheet?
08:48 07 Feb 2017

I want to make an Android app where Google Spreadsheet will be my server. I am able to post data to the server (Google spreadsheet) via Google Form, but I am unable to get data from that spreadsheet. My code is

public class ContactActivity extends ActionBarActivity {

    public static final MediaType FORM_DATA_TYPE
            = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
        public static final String URL="https://docs.google.com/forms/d/e/" +
            "XXXXXXXXX/formResponse";
        public static final String NAME="entry.XXXXXXXX";
    public static final String PHONE="entry.XXXXXXXX7";
    public static final String EMAIL="entry.XXXXXXXX";

    private  Context context;
    private EditText name;
    private EditText ph;
    private EditText email;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);
        context =this;

        Button sendButton = (Button)findViewById(R.id.submit);
        name = (EditText)findViewById(R.id.name);
        ph = (EditText)findViewById(R.id.phone);
        email = (EditText)findViewById(R.id.email);

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(TextUtils.isEmpty(name.getText().toString()) ||
                        TextUtils.isEmpty(ph.getText().toString()) ||
                        TextUtils.isEmpty(email.getText().toString()))
                {
                    Toast.makeText(context,"All fields are mandatory.",Toast.LENGTH_LONG).show();
                    return;
                }
                if(!android.util.Patterns.EMAIL_ADDRESS.matcher(email.getText().toString()).matches())
                {
                    Toast.makeText(context,"Please enter a valid email.",Toast.LENGTH_LONG).show();
                    return;
                }

                PostDataTask postDataTask = new PostDataTask();

                postDataTask.execute(URL,name.getText().toString(),ph.getText().toString(),email.getText().toString());
            }
        });
    }

    private class PostDataTask extends AsyncTask {

        @Override
        protected Boolean doInBackground(String... contactData) {
            Boolean result = true;
            String url = contactData[0];
            String n = contactData[1];
            String p = contactData[2];
            String e = contactData[3];
            String postBody="";

            try {
                
                postBody = NAME+"=" + URLEncoder.encode(n,"UTF-8") +
                        "&" + PHONE + "=" + URLEncoder.encode(p,"UTF-8") +
                        "&" + EMAIL + "=" + URLEncoder.encode(e,"UTF-8");
            } catch (UnsupportedEncodingException ex) {
                result=false;
            }

            try{

                OkHttpClient client = new OkHttpClient();
                                RequestBody body = RequestBody.create(FORM_DATA_TYPE, postBody);
                Request request = new Request.Builder()
                        .url(url)
                        .post(body)
                        .build();
                Response response = client.newCall(request).execute();
            }catch (IOException exception){
                result=false;
            }
            return result;
        }

        @Override
        protected void onPostExecute(Boolean result){
                        Toast.makeText(context,result?"Message successfully sent!":"There was some error in sending message. Please try again after some time.",Toast.LENGTH_LONG).show();
        }

    }
}
android google-sheets google-forms