0
I am trying to send a string from an Android app to a PHP page, but when I send it, an error occurs in the method
Httpresponse Response=httpclient.execute(httppost);
In case anyone can help me with any information to try to solve this problem, I thank you in advance!
I have done a good search on the internet and one of the most valid ways I found for my situation was this method below
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 String.
$tudo = $_REQUEST['legume'];
var_dump($tudo);
$fp = fopen('teste/teste.txt', 'w');
fwrite($fp, $tudo);
Thank you for your attention.
What mistake happens? what he says?
– rray
it gives the error of catch (Exception e) { Log. e("log_tag", "Error in http Connection " + e.toString()); } when it arrives at this method Httpresponse Sponse = httpclient.execute(httppost);
– CristianCotrena
What message?
– rray
E/log_tag: Error in http Connection android.os.Networkonmainthreadexception
– CristianCotrena
Onmainthreadexception means that you cannot run httpclient on the main thread. To do this create a
AsyncTask
and execute your code within Asynctask– LeoSantana
Thanks guys, I’ll search a way to do with Async Task and see if it works. anything I come back here. Valew support you!!
– CristianCotrena
Unfortunately I can only test tomorrow, but that error is really gone, Thank you very much for the help :D :)
– CristianCotrena
Related Networkonmainthreadexception error using Httpclient
– ramaral