吐司没有显示

时间:2014-07-26 11:00:36

标签: android toast

在调用服务时获得结果..如果结果字符串得到等于20然后我想显示一个吐司....我得到结果但是不能显示吐司消息... 请找到以下代码.. 能够显示System.out.println("检查值");但不是敬酒......

    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;
}

2 个答案:

答案 0 :(得分:3)

您无法在后台线程上更新UI。在后台线程上调用新线程中的WebService调用。因此,您必须在UI线程上更新UI。

检查此代码: -

    runOnUiThread(new Runnable() {

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

答案 1 :(得分:1)

if the result string got is equal to 20

所以

if ("20".equals(result))

而不是

if (valueLength == 20){
相关问题