Search code examples
androidtoast

Toast is not displaying


while calling a service am getting the result ..if the result string got is equal to 20 then i want to display a toast....am getting result but not able to show toast message... please find the below code.. able to display a System.out.println("check values "); but not toast...

    private String serviceCalling() {

    // TODO Auto-generated method stub
    int responseCode = 0;
    JSONObject jObjet = null;

    String result = "";
    InputStream is = null;

    try {
        HttpClient httpclient = new DefaultHttpClient();
        // HttpPost httppost = new
        // HttpPost("");

        HttpPost httppost = new HttpPost("webservice url");

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

        responseCode = response.getStatusLine().getStatusCode();

        System.out.println("responseCode::" + responseCode);

    } catch (Exception e) {
        Log.e("NO CONNECTION", "Error in http connection " + e.toString());
    }

    // convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();

        result = sb.toString();


        int valueLength = result.length();

        if (valueLength == 20){

            Toast.makeText(LoginScreen.this, "Detilas Check", Toast.LENGTH_SHORT).show();


        }

        System.out.println("response" + result);
    } catch (Exception e) {
        Log.e("CANT CONVERT DATA",
                "Error converting result " + e.toString());
    }

    try {


            jObjet = new JSONObject(result);
            JSONArray jArray = jObjet.getJSONArray("login");
            JSONObject jsonObject = jArray.getJSONObject(0);
            String id = jsonObject.getString("id");
            String count = jsonObject.getString("my_ads_list_start_count");
            Intent intent = new Intent(LoginScreen.this, HomeScreen.class);
            intent.putExtra("ID", id);
            intent.putExtra("ADDCOUNT", count);


            startActivity(intent);
            finish();
//              System.out.println("id:::::::::" + id);
//              System.out.println("count:::::::::" + count);


    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result;
}

Solution

  • You cannot update UI on the background thread. WebService call in the new Thread is invoked on the background thread. So, you have to update UI on the UI thread.

    Check this code:-

        runOnUiThread(new Runnable() {
    
            @Override
            public void run() {
                Toast.makeText(LoginScreen.this, "Details Check", Toast.LENGTH_SHORT).show();
            }
        });