user4108483
user4108483

Reputation:

Java get result from Thread and Runnable

in this below code i can not get result from getValue from Foo class and thats return null but that must be return value. result is correct and this function could not return that. for example this is my class:

public class JsonService {
    private JSONObject json = new JSONObject();
    public JsonService(final String username, final String password) throws Exception{
        json.put("username", username);
        json.put("password", password);
    }
    public class Foo implements Runnable {
        private String result;

        @Override
        public void run() {
            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
            HttpResponse response;

            try {
                HttpPost post = new HttpPost("http://www.example.com/json_android.php");
                StringEntity se = new StringEntity( json.toString());
                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                post.setEntity(se);
                response = client.execute(post);
                if(response!=null){
                    InputStream stream = response.getEntity().getContent();
                    result = convertToString(stream);

                    /*
                         I can Log result with below line
                    */
                    Log.e("response is: ", result);

                    /*
                         result is {"username = ":"Hello"}
                    */
                }
            } catch(Exception e) {
                e.printStackTrace();
                Log.e("Error", String.valueOf(e));
            }
        }

        public String getValue() {
            /*
                 return in this fucntion not working
            */
            return result;
        }
    }
    public String request() throws ExecutionException, InterruptedException {
        Foo foo = new Foo();
        new Thread(foo).start();
        return foo.getValue();
    }

How to get result correctly from Foo and return that from reauest() ? please help me. thanks

Upvotes: 2

Views: 2096

Answers (2)

mmlooloo
mmlooloo

Reputation: 18977

the method of get from class FutureTask (at the above answer return fooFuture.get();) is a blocking function that means when you call it from UI thread it blocks the UI thread so cause your app sends you ANR error. My suggestion to solve it is using LocalBroadCastManager and when the computation finished it sends a local broadcast and in your UI you will get the result at the onReceive from the Extra that is embedded in the intent. another solution is using ruOnUiThread or passing the handler from UI thread. and last one is using post method of widgets.

Upvotes: 0

René Link
René Link

Reputation: 51353

Use a FutureTask and implement a Callable.

public class Foo implements Callable<String> {

    @Override
    public String call() {
        String result = null;
        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
        HttpResponse response;

        try {
            HttpPost post = new HttpPost("http://www.example.com/json_android.php");
            StringEntity se = new StringEntity( json.toString());
            se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            post.setEntity(se);
            response = client.execute(post);
            if(response!=null){
                InputStream stream = response.getEntity().getContent();
                result = convertToString(stream);

                /*
                     I can Log result with below line
                */
                Log.e("response is: ", result);

                /*
                     result is {"username = ":"Hello"}
                */
            }

        } catch(Exception e) {
            e.printStackTrace();
            Log.e("Error", String.valueOf(e));
        }
        return result;
    }
}

and than use it with the FutureTask

public String request() throws ExecutionException, InterruptedException {
   Foo foo = new Foo();
   FutureTask<String> fooFuture = new FutureTask<String>(foo);
   new Thread(fooFuture).start();
   return fooFuture.get();
}

Upvotes: 2

Related Questions