无法在Google电子表格中获得回复

时间:2018-10-08 14:44:04

标签: java android http okhttp

我正在创建一个应用程序以获取用户反馈,并创建一个Google表单以从电子表格中的应用程序获取数据。 应用程序编译成功,当我填写表单并提交时...我获得了成功的响应,但是当我打开相同的Google表单或电子表格时。没有记录任何响应。

这是我的主要活动

public class MainActivity extends AppCompatActivity {

    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/e/1FAIpQLSdatlKheW5G_H8g9DCD8_2K1Ltytb4Fn64erYleewN8PWB1vQ/viewform";
    //input element ids found from the live form page
    public static final String EMAIL_KEY="entry.1725271100";
    public static final String SUBJECT_KEY="entry.161281878";
    public static final String MESSAGE_KEY="entry.1798953063";

    private  Context context;
    private EditText emailEditText;
    private EditText subjectEditText;
    private EditText messageEditText;

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

        //save the activity in a context variable to be used afterwards
        context =this;

        //Get references to UI elements in the layout

        emailEditText = (EditText)findViewById(R.id.email_input);
        subjectEditText = (EditText)findViewById(R.id.name_input);
        messageEditText = (EditText)findViewById(R.id.feedback_input);

        findViewById(R.id.submit_button).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();
        }

    }
}

请帮助
提前致谢。

0 个答案:

没有答案