Return onPostExecute() String

Asked

Viewed 92 times

0

I tried using the @ramaral tip Original post but my code has build errors that I don’t know how to delete.

1-In the interface statement the error appears:

Unexpected interface

.

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

Thank you in advance for your attention.

Follows 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;

        }   

}

1 answer

0

You have to implement the interface created in Asynctask in the Mainactivity statement. For example:

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

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

  • I must be making some silly mistake. Unknown Entity error in the mainactivity statement.

  • I edited my answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.