I have tried many different ways to attempt to upload data from my Android app to a PHP script on my server using HttpURLConnection, but nothing shows up in the file that is created by PHP on the server. I had success using HTTPClient, but I had to make the switch to using HttpURLConnection. The app does NOT crash when it is run. I am sure that there is something simple that I am overlooking. My PHP script works fine, and even returns the expected response, but there is something I don't yet see that is wrong with my Android Code. Any help is apprecieated.
Here is the beginning of the PHP script:
$data = $_POST["deviceSIG"];
Here is the code I am using to upload my data to the PHP script:
// the string deviceSIG is defined elsewhere and has been defined in the class.
private class MyAsyncTask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params)
try{
URL url = new URL("http://192.168.10.199/user_script.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
OutputStream outputStream = conn.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
writer.write(deviceSIG);
writer.close();
outputStream.close();
// read response
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) { response.append(inputLine); }
in.close();
result = response.toString();
// disconnect
conn.disconnect();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//-------------------------------------------------------------------------------
protected void onProgressUpdate(Integer... progress){
progBar.setProgress(progress[0]);
}
//-------------------------------------------------------------------------------
protected void onPostExecute(String result){
progBar.setVisibility(View.GONE);
String rawEcho = result;
String[] Parts = rawEcho.split("~");
String echo = Parts[1];
String UIID = "User ID: " + echo;
try {
FileOutputStream fOS = openFileOutput("Info.txt", Context.MODE_APPEND);
fOS.write(newLine.getBytes());
fOS.write(UIID.getBytes());
fOS.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}