Error while executing an HttpResponse in Android Studio

0

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.

    
asked by anonymous 05.11.2015 / 18:51

1 answer

1

Give it to me !! working!

I made some changes to the scope and used some tips provided by LeoSantana and worked out right here

the code stayed like this

public void postData0(View v){new LongOperation().execute("");}


private class LongOperation extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        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"));
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("legume", "{\"usuario\":[{\"nome\":\"fulano\",\"idade\":\"97\",\"email\":\"[email protected]\", \"sexo\":\"masculino\"}]}"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            Log.v("GG", "erro ocorre na linha abaixo. . .");

            HttpResponse response = httpclient.execute(httppost);
            Log.v("GG", ":D :D :D . . .");
            HttpEntity entity = response.getEntity();
            Log.v("GG", "1 . . .");
            // end - line is for sever connection/communication
            InputStream is = entity.getContent();
            Log.v("GG", "2 . . .");
            //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());
        }


        return null;
    }

    @Override
    protected void onPostExecute(String result) {
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}

Thanks for the help: D

    
06.11.2015 / 14:06