Return String from onPostExecute ()

0

I tried to use @ramaral's hint Original post but my code is with compilation errors that I do not know how to delete.

1-In the interface declaration the error appears:

  

unexpected interface

.

2-Should the onPostCompleted method be internal or external to onCreate ?

Thank you in advance for your attention.

Follow the code:

    public class MainActivity extends Activity {

    // variável de instância para o player
    private MediaPlayer mp = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    //new PutMethodDemo().execute();

    @Override
    public void onPostCompleted(String result){

        //procurar pelo texto rbtnhora
        if(!result.contains("rbtnhora\" value=\"-1\""))
            Log.e("Response_agenda  ", "Agenda fechada");
        else
        {
            Log.e("Response_agenda  ", "Agenda aberta");

        }

    }

    PutMethodDemo lb = new PutMethodDemo();
    lb.setOnPostCompletedListener(MainActivity.this);
    lb.execute();

}

    private class PutMethodDemo extends AsyncTask<String , Void ,String>
{

    HttpResponse response;
    String server_content = null;
    HttpClient client = getThreadSafeClient();

    private OnPostCompletedListener onPostCompletedListener;


    public interface OnPostCompletedListener{
        void onPostCompleted(String result);
    }

    public void setOnPostCompletedListener(OnPostCompletedListener onPostCompletedListener){
        this.onPostCompletedListener = onPostCompletedListener;

       }

    public DefaultHttpClient getThreadSafeClient()  {

        DefaultHttpClient client = new DefaultHttpClient();
        ClientConnectionManager mgr = client.getConnectionManager();
        HttpParams params = client.getParams();
        client = new DefaultHttpClient(new ThreadSafeClientConnManager(params,   mgr.getSchemeRegistry()), params);
        return client;
    } 


    @Override
    protected String doInBackground(String... strings) {

        HttpPost post = new HttpPost("http://www.consuladoportugalrjservicos.com/public_html/exec");

        doGet("http://consuladoportugalrj.org.br");     
        doGet("http://www.consuladoportugalrjservicos.com/public_html/");

        //post
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();

        pairs.add(new BasicNameValuePair("modulo", "modulo.login"));
        pairs.add(new BasicNameValuePair("acao", "login101"));

        try
        {
            post.setEntity(new UrlEncodedFormEntity(pairs));
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }

        try
         {
         response = client.execute(post);
         }
         catch (IOException e)
         {

         e.printStackTrace();
         }



        // Getting the status code.
        int statusCode = response.getStatusLine().getStatusCode();

        Log.e("Response_code  ", "" + statusCode);

        HttpEntity httpEntity = response.getEntity();


        try
        {
            server_content = readStream(httpEntity.getContent());
        }
        catch (UnsupportedOperationException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        //post 2
        pairs.clear();
        pairs.add(new BasicNameValuePair("modulo", "modulo.servicos"));
        pairs.add(new BasicNameValuePair("acao", "servico990"));


        try
        {
            post.setEntity(new UrlEncodedFormEntity(pairs));
        }
        catch (UnsupportedEncodingException e)
        {       
            e.printStackTrace();
        }

        try
        {
            response = client.execute(post);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        // Getting the status code.
        statusCode = response.getStatusLine().getStatusCode();

        Log.e("Response_code  ", "" + statusCode);

        httpEntity = response.getEntity();

        try
        {
            server_content = readStream(httpEntity.getContent());
        }
        catch (UnsupportedOperationException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        Log.e("server_content",  server_content);

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        if(onPostCompletedListener != null){
            //Chama o listener passando a string
            onPostCompletedListener.onPostCompleted(s);
        }   

    }

    // Converting InputStream to String
    private String readStream(InputStream in) {
            BufferedReader reader = null;
            StringBuffer response = new StringBuffer();
            try {
                reader = new BufferedReader(new InputStreamReader(in));
                String line = "";
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return response.toString();
        }


    private HttpResponse doGet(String enderecoGet)
    {

        HttpGet get = new HttpGet(enderecoGet);

        try
        {
        response = client.execute(get);
        }
        catch (IOException e)
        {       
        e.printStackTrace();
        }


        // Getting the status code.
        int statusCode = response.getStatusLine().getStatusCode();

        Log.v("Response_source  ", "" + enderecoGet);
        Log.v("Response_code  ", "" + statusCode);  


        return response;

        }   

}
    
asked by anonymous 15.05.2017 / 13:26

1 answer

0

You have to implement the interface created in AsyncTask in the MainActivity declaration. For example:

public class MainActivity extends Activity implements PutMethodDemo.OnPostCompletedListener {
...
}

Also pass the construction of the onPostCompleted method out of OnCreate.

    
15.05.2017 / 16:39