我的天气应用程序崩溃了

时间:2017-11-22 11:04:40

标签: java android

我正在尝试制作天气应用。如果您输入城市名称,它会为您提供天气信息,但如果输入错误的城市名称,应用程序会崩溃。

        protected String doInBackground(String... urls) {

        String result = "";
        URL url;
        HttpURLConnection urlConnection = null;

        try {
            url = new URL(urls[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            int data = reader.read();
            while (data != -1){
                char current = (char) data;
                result += current;
                data = reader.read();

            }
            return result;


        } catch (RuntimeException e){
            Toast.makeText(getApplicationContext(), "Could not find weather", Toast.LENGTH_LONG);

        } catch(Exception e) {
                Toast.makeText(getApplicationContext(), "Could not find weather", Toast.LENGTH_LONG);
        }

        return null;

错误

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
              Process: com.example.android.whatstheweather, PID: 10206
              java.lang.RuntimeException: An error occurred while executing doInBackground()
                  at android.os.AsyncTask$3.done(AsyncTask.java:325)

4 个答案:

答案 0 :(得分:2)

吐司问题,使用UI线程显示吐司

Activity_Name.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {

           Toast.makeText(getApplicationContext(), "Could not find weather", Toast.LENGTH_LONG).show();


        }
    });

答案 1 :(得分:2)

boolean flag=false; // declare boolean  at the top of the class it's a class variable



public class Download extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
        String result = "";
        URL url;
        HttpURLConnection urlConnection = null;

        try {
            url = new URL(urls[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            int data = reader.read();
            while (data != -1){
                char current = (char) data;
                result += current;
                data = reader.read();

            }
            if(result.isEmpty()){
            flag=false;
            }else{
            flag=true;
            }   
            return result;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if(!flag){
                Toast.makeText(getApplicationContext(), "Could not find weather", Toast.LENGTH_LONG).show();
            }

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }
    }

答案 2 :(得分:1)

您可以显示可供选择的城市名称列表,而不是输入键入的输入,这样就不会输入错误或不正确的输入。

创建一个城市名称检查以查看它是否是有效的城市名称,如果不是,请不要让它继续下去。甚至可能通过匹配拼写来提供城市名称的建议。

答案 3 :(得分:1)

你需要在主线程中使用Toast,使用runOnUiThread,或者返回一个字符串并在你的Activity中用toast打印返回。