无法在吐司中显示吐司

梁志强
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));

            }
    }

}

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

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

代码潜水

请尝试以下操作:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章