I am attempting to send a string
to a PHP
file on a remote server using POST
and `GET, the PHP then writes the string to a text file.
My code (below) compiles and runs without error, however when the submit button is pressed there is no action. I.e. the toast
does not display and when I check the text file
that is written to from my PHP
file on the server, there is nothing.
Am I missing something very obvious that would cause this not to work? (note: "my_url" is there on purpose, that is not the error)
Android Code:
public class MainActivity extends AppCompatActivity {
@Bind(R.id.tvTitle)
TextView title;
@Bind(R.id.etName)
EditText name;
@Bind(R.id.etEmail)
EditText email;
@Bind(R.id.etIdea)
EditText idea;
@Bind(R.id.btnSubmit)
Button submit;
String toSubmit = "";
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
submit.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try{
new MyTask().execute();
}catch(Exception e){
e.printStackTrace();
}
}
});
}
public static long copyLarge(InputStream input, OutputStream output)
throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
public static int copy(InputStream input, OutputStream output) throws IOException {
long count = copyLarge(input, output);
if (count > Integer.MAX_VALUE) {
return -1;
}
return (int) count;
}
private class MyTask extends AsyncTask<String, Void, String>
{
boolean success = false;
@Override
protected String doInBackground(String... params) {
StringBuilder respData = new StringBuilder();
InputStream stream = null;
OutputStream os = null;
HttpURLConnection httpUrlConnection;
URLConnection conn;
URL url;
try {
url = new URL("my_url/recieveString.php");
conn = url.openConnection();
httpUrlConnection = (HttpURLConnection) conn;
httpUrlConnection.setUseCaches(false);
//httpUrlConnection.setRequestProperty("User-Agent", "App");
httpUrlConnection.setConnectTimeout(30000);
httpUrlConnection.setReadTimeout(30000);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setDoOutput(true);
os = httpUrlConnection.getOutputStream();
toSubmit = "test";
stream = new ByteArrayInputStream(toSubmit.getBytes(StandardCharsets.UTF_8));
copy(stream, os);
httpUrlConnection.connect();
int responseCode = httpUrlConnection.getResponseCode();
if (200 == responseCode) {
InputStream is = httpUrlConnection.getInputStream();
InputStreamReader isr = null;
try {
isr = new InputStreamReader(is);
char[] buffer = new char[1024];
int len;
while ((len = isr.read(buffer)) != -1) {
respData.append(buffer, 0, len);
}
} finally {
if (isr != null) {
isr.close();
success = true;
}
}
is.close();
} else {
// use below to get error stream
//inputStream = httpUrlConnection.getErrorStream();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
stream.close();
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
return "done";
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast toast = Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT);
}
}
}
PHP file:
<?php
$stringRecieved=$_GET["toSubmit"];
$file = 'list.txt';
// Write the contents to the file,
file_put_contents($file, $stringRecieved, FILE_APPEND | LOCK_EX);
?>
If you are POSTing to the server the variable will not be in GET.
Incidentally, it is usually a good idea to provide a wrapper and test that POST is actually being used; it's usually a good idea to sanitize input, and it's usually a good idea to make sure that access is coming from a valid source, none of which your PHP seems to be doing yet.