I have code to post data to a server, but sometimes, if the network is slow, the data does not get through. My app is somewhat similar to a messenger program, and I want to post and store every message in a MySQL database. My client-side code is shown below:
try{
// URLEncode user defined data
String loginValue = URLEncoder.encode(login.getText().toString(), "UTF-8");
String fnameValue = URLEncoder.encode(fname.getText().toString(), "UTF-8");
String emailValue = URLEncoder.encode(email.getText().toString(), "UTF-8");
String passValue = URLEncoder.encode(pass.getText().toString(), "UTF-8");
// Create http cliient object to send request to server
HttpClient Client = new DefaultHttpClient();
// Create URL string
String URL = "http://****/httpget.php?user="+loginValue+"&name="+fnameValue+"&email="+emailValue+"&pass="+passValue;
//Log.i("httpget", URL);
try
{
String SetServerString = "";
// Create Request to server and get response
HttpPost httppost = new HttpPost(URL);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
SetServerString = Client.execute(httppost, responseHandler);
// Show response on activity
content.setText(SetServerString);
}
catch(Exception ex)
{
content.setText("Fail!");
}
}
catch(UnsupportedEncodingException ex)
{
content.setText("Fail");
}
... and here is my server-side PHP code:
<?php
$name = urldecode($_POST['name']);
$user = urldecode($_POST['user']);
$email = urldecode($_POST['email']);
$pass = urldecode($_POST['pass']);
print " ==== GET REQUEST DATA =====
Name: $name
Email: $email
User: $user
Pass: $pass";
?>