How to pass a class as a parameter in another class and return a variable that is within a Thread

Asked

Viewed 269 times

1

I work with Delphi and I decided to learn how to make java applications, and I came across a problem.

I have this class

    public class Dados {
    public void getJson(final String url) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Object retorno = null;
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpGet request       = new HttpGet();

                    request.setURI(new URI(url));

                    HttpResponse response = httpclient.execute(request);
                    InputStream content   = response.getEntity().getContent();
                    Reader reader         = new InputStreamReader(content);

                    Gson gson = new Gson();
                    retorno   = gson.fromJson(reader, HashMap.class);

                    content.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

The upper class works, but I’d like to do two more things on it. First I would like to pass a class (any class) as parameter and then return what is in the "return" variable, for example:

 public class Dados {
    public Class<T> getJson(Class<T> classeTal, final String url) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Object retorno = null;
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpGet request       = new HttpGet();

                    request.setURI(new URI(url));

                    HttpResponse response = httpclient.execute(request);
                    InputStream content   = response.getEntity().getContent();
                    Reader reader         = new InputStreamReader(content);

                    Gson gson = new Gson();
                    retorno   = gson.fromJson(reader, classeTal.class);

                    content.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    return retorno;
    }
}

Could someone tell me how I would do this and if you could give me an example? For the example I gave does not work.

  • This can be done but you can use the class Asynctask that allows this and something else.

  • Beauty @ramaral I will try to apply the Asynctask class and see if it solves my problem.

1 answer

0


I think you can try with Generics, for example:

public class Dados<T> implements Runnable{
    @Override
    public void run() {
        T object = (T)new Object(); 
    }
}

public class App {
    public static void main(String[] args) {
        Dados dados = new Dados<String>();
    }
}
  • Thanks, I used your tip joining with Asynctask and it worked out I was able to pass the class as parameter.

Browser other questions tagged

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