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/[email protected]/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));

            }
    }

}


我如何才能真正展示吐司而不停下来?每当它落入陷阱时,它都不会响应。

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

最佳答案

请尝试以下操作:


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

}

getData()中:

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

onPostExecute中:

if(result == null) {
        return;
    }

07-27 15:56