I'm trying to send data from Android to a script in a google Spreadsheet via a POST request. I get no errors but the request doesn't seem to go through.
I've tried some of the solutions provided here
I tried the script with a simple form and it works, but from android it doesn't look like anything is even trying to get to it.
I use 2 classes for this, one extends AppCompatActivity
and the other extends AsyncTask
public class FormActivity extends AppCompatActivity {
// STUFF
private void sendData() {
// Post Request
int data1 = SOMEDATA1;
int data2= SOMEDATA2;
String sdata1 = Integer.toString(data1 );
String sdata2 = Integer.toString(data2);
String urlString = MYSCRIPTURL; // URL to call
String data = "data1=" + data1 + "&data2=" + data2 ; //data to post
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute(urlString, data);
}
}
This is the class that should do the POST request
public class MyAsyncTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String urlString = params[0]; // URL to call
String data = params[1]; //data to post
OutputStream out = null;
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
out = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(data);
writer.flush();
writer.close();
out.close();
urlConnection.connect();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
}
I have also added
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
to the manifest.
This seems to run fine until the end but on the side of the script the log shows nothing.
The method doInBackground
in the answer to the question I linked is void but Android Studio only accepts it as string for me. Apart from that and a few additions the code is the same.
EDIT: I also tried using GET, doesn't work.