title: How to update UI from another Thread using handler in android?
body: <p>I am developing android app in which I have to send some data to server and receive some response from server for this I am using AsynkTask. Right now I am getting response in onPostexecute method of Asynktask and update UI according to server response in onPostexecute method of AsynkTask. what I want is to update UI not from onPostexecute method but from another thread using handler to reduce load from main Thread.How can I do that kindly tell me . </p>
  
  <p>here is my code of AsynkTask:-</p>
  
  <pre><code>private class LoginAttempt extends AsyncTask&lt;String, Void, String&gt; {
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          CProgressBar.getInstance().showProgressBar(getActivity(), "Please wait while Logging...");// showing progress ..........
      }
  
      @Override
      protected String doInBackground(String... params) {
          getLoginDetails();// getting login details from editText...........
          InputStream inputStream = null;
          m_oJsonsResponse = new CJsonsResponse();
          isFirstLogin = true;
          try {
              // 1. create HttpClient
              HttpClient httpclient = new DefaultHttpClient();
              // 2. make POST request to the given URL
              HttpPost httpPost = new HttpPost(s_szLoginUrl);
              String json = "";
              // 3. build jsonObject
              JSONObject jsonObject = new JSONObject();
              jsonObject.put("agentCode", s_szMobileNumber);
              jsonObject.put("pin", s_szPassword);
              jsonObject.put("firstloginflag", m_oLoginSession.isLogin());
              // 4. convert JSONObject to JSON to String
              json = jsonObject.toString();
              // 5. set json to StringEntity
              StringEntity se = new StringEntity(json);
              // 6. set httpPost Entity
              httpPost.setEntity(se);
              // 7. Set some headers to inform server about the type of the content
              //  httpPost.setHeader("Accept", "application/json");   ///not required
              httpPost.setHeader("Content-type", "application/json");
              // 8. Execute POST request to the given URL
              HttpResponse httpResponse = httpclient.execute(httpPost);
              HttpEntity entity = httpResponse.getEntity();
              // 9. receive response as inputStream
              inputStream = entity.getContent();
              System.out.print("InputStream...." + inputStream.toString());
              System.out.print("Response...." + httpResponse.toString());
  
              StatusLine statusLine = httpResponse.getStatusLine();
              System.out.print("statusLine......" + statusLine.toString());
              ////Log.d("resp_body", resp_body.toString());
              int statusCode = statusLine.getStatusCode();
              if (statusCode == 200) {
                  // 10. convert inputstream to string
                  if (inputStream != null) {
                      s_szresult = m_oJsonsResponse.convertInputStreamToString(inputStream);
                  }
              } else
                  s_szresult = "Did not work!";
  
          } catch (Exception e) {
              Log.d("InputStream", e.getLocalizedMessage());
          }
          return s_szresult;
      }
  
      @Override
      protected void onPostExecute(final String response) {
          super.onPostExecute(response);
          new Handler(Looper.getMainLooper()).post(new Runnable() {
              @Override
              public void run() {
                  CProgressBar.getInstance().hideProgressBar();// hide progressbar after getting response from server......
                  try {
                      m_oResponseobject = new JSONObject(response);// getting response from server
  
                      getResponse();// getting response from server ........
  
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
              }
  
          });
      }
  
      public void getResponse() throws JSONException {
          if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Transaction Successful")) {
              m_oLoginSession.setLoginData(s_szResponseMobile, s_szResponsePassword);
              getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, new CDealMainListing()).commit();
              CToastMessage.getInstance().showToast(getActivity(), "You are successfully Logged In");
          } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Agentcode Can Not Be Empty")) {
              CToastMessage.getInstance().showToast(getActivity(), "Please Enter Valid Mobile Number");
          } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Pin Can Not Be Empty")) {
              CToastMessage.getInstance().showToast(getActivity(), "Please Enter Password");
          } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Invalid PIN")) {
              CToastMessage.getInstance().showToast(getActivity(), "Please enter correct Password");
          } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Subscriber/Agent Blocked due to Wrong Attempts")) {
              CToastMessage.getInstance().showToast(getActivity(), "You are blocked as You finished your all attempt");
          } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {
              CToastMessage.getInstance().showToast(getActivity(), "Connection Lost ! Please Try Again");
          } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Subscriber/Agent Not Found")) {
              CToastMessage.getInstance().showToast(getActivity(), "User not found ! Kindly Regiter before Login");
          } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("OTP not verify")) {
              CToastMessage.getInstance().showToast(getActivity(), "Otp not Verify ! Kindly Generate Otp on Sign Up");
          }
      }
  </code></pre>