无法在捕获中显示吐司

时间:2014-07-21 03:36:14

标签: android

package com.example.m2mai;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;


public class RetrieveActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve);
    }



    public void getStream(View v)
    {   
        new MyAsyncTask().execute();
    }

private class MyAsyncTask extends AsyncTask<String, Void, String>
{   
    ArrayList<String> mNameList = new ArrayList<String>();
    public ArrayList<String> atList=new ArrayList<String>();
    public ArrayList<String> dataList=new ArrayList<String>();


        protected String doInBackground(String... params) 
        {
            return getData();

        }
        public long getDateTo() 
      {
        EditText toText = (EditText)findViewById(R.id.dateTo);
        String To = toText.getText().toString();

        DateFormat dateFormatTo = new SimpleDateFormat("dd/MM/yyyy");
        Date dateTo = null;
        try {
            dateTo = dateFormatTo.parse(To);
        } catch (java.text.ParseException e) {
            .runOnUiThread(new Runnable(){
                  public void run() {
                      Toast.makeText(getApplicationContext(),"Please key in the correct input",    Toast.LENGTH_LONG).show();
                  }
                });
            e.printStackTrace();
        }
        long timeTo = dateTo.getTime();
        new Timestamp(timeTo);  
        return timeTo/1000;

      }

        protected String getData()
        {
            String toTS = ""+getDateTo();
            String decodedString="";
            String returnMsg="";
            String request = "http://api.carriots.com/devices/defaultDevice@eric3231559.eric3231559/streams/?order=-1&max=10&at_to="+toTS;
            URL url;
            HttpURLConnection connection = null;  
            try {
                url = new URL(request); 
                connection = (HttpURLConnection) url.openConnection();

                connection.addRequestProperty("carriots.apikey", "1234567");
                connection.addRequestProperty("Content-Type", "application/json");
                connection.setRequestMethod("GET"); 

                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                while ((decodedString = in.readLine()) != null) 
                {
                    returnMsg+=decodedString;
                }
                in.close();
                connection.disconnect();
                JSONObject nodeRoot = new JSONObject(returnMsg); 
                JSONArray res = nodeRoot.getJSONArray("result");

                 for (int i = 0; i < res.length(); i++) 
                    {
                    JSONObject childJSON = res.getJSONObject(i);
                    if (childJSON.get("data")!=null)
                        {
                       String value = childJSON.getString("data");
                       dataList.add(value);
                       JSONObject node=new JSONObject(value);
                       atList.add(node.get("temperature").toString()); 
                        }
                    } 
                 }
                 catch (Exception e) 
            {
                e.printStackTrace(); 
                returnMsg=""+e;

            }
            return returnMsg;
        }

        protected void onPostExecute(String result)
            {


            for(int i = 0; i < atList.size(); i++)
            {
            ListView mainListView = (ListView) findViewById(R.id.listView1);


            ArrayAdapter<String>  mArrayAdapter = new ArrayAdapter<String>(RetrieveActivity.this,android.R.layout.simple_list_item_1,mNameList);


            mainListView.setAdapter(mArrayAdapter);

            mNameList.add(atList.get(i).toString());
            mArrayAdapter.notifyDataSetChanged();
            }
            Toast.makeText(getApplicationContext(),result, Toast.LENGTH_SHORT).show();
            EditText myData1=(EditText)findViewById(R.id.editText1);
            myData1.setText(atList.get(0));

            }
    }

}

如何在不停止播放的情况下实际显示吐司?每当它陷入捕获时它就没有响应。

............................................... .................................................. .................................................. .................................................. .................................................. .....................

3 个答案:

答案 0 :(得分:4)

如果您在try-catch中使用worker thread并希望显示Toast,那么我担心它无法正常工作。您必须在Main(UI) thread

中执行此操作

请尝试以下操作:

try {
    dateTo = dateFormatTo.parse(To);
} 
catch (java.text.ParseException e) {

your_activity_context.runOnUiThread(new Runnable(){
  public void run() {
      Toast.makeText(getApplicationContext(),"Please key in the correct input",    Toast.LENGTH_LONG).show();
  }
});

e.printStackTrace();
}

答案 1 :(得分:1)

请尝试以下操作:

  1. getDateTo()

    try {
        dateTo = dateFormatTo.parse(To);
    } catch (java.text.ParseException e) {
        runOnUiThread(new Runnable(){
              public void run() {
                  Toast.makeText(getApplicationContext(),"Please key in the correct input",    Toast.LENGTH_LONG).show();
              }
            });
        e.printStackTrace();
        return -1L; // attention here
    
    }
    
  2. getData()

       long dateTo = -1L;
        if((dateTo = getDateTo()) == -1L){
            return null;
        }
        String toTS = "" + getDateTo();
    
  3. onPostExecute

    if(result == null) {
            return;
        }
    

答案 2 :(得分:0)

使boolean flag = false; 全局

捕获块内部,使flag = true;

if块之内运行Toast

if (flag) {
Toast.makeText(this, "Sorry, Couldn't find anything!", Toast.LENGTH_LONG).show();
}

在您的onclick方法内部。