I'm trying to send a string from an Android application to a PHP page, but when I send it, an error occurs in the
HttpResponse response = httpclient.execute (httppost);
If someone can help me with any information to try to solve this problem, thank you in advance!
I've already done a good search on the internet and one of the most valid ways I found for my situation was this method below
reference: link
public void postData0(View v){
try {
Log.v("GG", "Sending sever 1 - try");
// start - line is for sever connection/communication
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://meuSite.com.br/listener.php");
httppost.setEntity(new StringEntity("legume = batata"));
Log.v("GG", "erro ocorre na linha abaixo. . .");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// end - line is for sever connection/communication
InputStream is = entity.getContent();
Toast.makeText(getApplicationContext(),
"Send to server and inserted into mysql Successfully", Toast.LENGTH_LONG)
.show();
// Execute HTTP Post Request
response= httpclient.execute(httppost);
entity = response.getEntity();
String getResult = EntityUtils.toString(entity);
Log.e("response =", " " + getResult);
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "
+ e.toString());
}
}
And this is the code I made to receive the String.
$tudo = $_REQUEST['legume'];
var_dump($tudo);
$fp = fopen('teste/teste.txt', 'w');
fwrite($fp, $tudo);
Thanks for your attention.