In android there is a need to perform any interactions with the “Internet” in another stream, but sometimes there is a need to wait for the result from the server and then do something with it and of course I would like not to barter any extra crutches in the main code to interact with components. A little thought decided to do something like this:
private void initRequest(){ final Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { Toast.makeText(MainActivity.this, (String) msg.obj, Toast.LENGTH_LONG).show(); }; }; Thread thread = new Thread(new Runnable() { @Override public void run() { Message message; if(Utils.isNetworkConnected(MainActivity.this)){ HttpClient httpclient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(INIT_HOST); httpGet.addHeader("Content-Type", "application/json"); try { HttpResponse httpResponse = httpclient.execute(httpGet); String strEntity = EntityUtils.toString(httpResponse.getEntity()); JSONObject jsonObject = new JSONObject(strEntity); Global_Variables.COORDS_INTERVAL = jsonObject.getInt(Constance.INIT_RECORD_INTERVAL); Global_Variables.POST_INTERVAL = jsonObject.getInt(Constance.INIT_SEND_INTERVAL); Global_Variables.TIME_CHECK_SPEED = jsonObject.getInt(Constance.INIT_SPEED_INTERVAL); Global_Variables.TRIGGER_SPEED = jsonObject.getInt(Constance.INIT_TRIGGER_SPEED); } catch (ClientProtocolException e) { message = new Message(); message.obj = "Failed send init request!"; handler.sendMessage(message); } catch (IOException e) { message = new Message(); message.obj = "Unsuccessful receiving entity!"; handler.sendMessage(message); } catch (JSONException e) { message = new Message(); message.obj = "Failed parse Json!"; handler.sendMessage(message); } }else{ message = new Message(); message.obj = "Check Internet connection!"; handler.sendMessage(message); } } }); thread.setName("Init request"); thread.start(); try { thread.join(); } catch (InterruptedException e) { throw new RuntimeException("Thread "+thread.getName()+" interrupted!"); } } For example, I took one of the functionals of my application. I don’t see any sense in the executed function itself, I would like to draw attention to such moments as: Handler (necessary for interacting with the UI thread), and the join () method for the thread that is needed to indicate the thread in which we started the thread thread , that it needs to wait for the thread to finish, this is necessary so that no one has time to use global data. What does all this give? And the fact that you quietly execute the method in another thread without unnecessary code in the main code and with all this the main thread is waiting for your completion, and as a result you can also interact with the UI stream. The only drawback of all this I see is that the result makes sense to send only at the end of the stream, for if it is done in the middle, then the code in the handler will not be executed due to join () until the entire thread does the work to the end. And now the question: what do you think about all this, did I come up with an interesting way or did I write some kind of nonsense?
AsyncTasknot fit? - Suvitruf ♦