在某些情况下,当按下按钮时什么也没有发生

问题描述 投票:0回答:1
private EditText editTextCity;
private  EditText editTextNumber;
private Spinner spinnerCounties;
private static boolean doesExist;
private static int NumberOfDays;
private static String city;
private static int id;
private static String number;

private static ArrayList<WeatherDay> weatherDays;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editTextCity = findViewById(R.id.editTextCity);
    editTextNumber=findViewById(R.id.editTextNumber);
    spinnerCounties=findViewById(R.id.spinner);
}

public void onClickCheckWeather(View view) throws ExecutionException, InterruptedException {
    city = editTextCity.getText().toString().trim();
    id =spinnerCounties.getSelectedItemPosition();
    number=editTextNumber.getText().toString();

    if (!city.isEmpty()&& !number.isEmpty()&&(Integer.parseInt(number)>0 && Integer.parseInt(number)<17)) {
        NumberOfDays=Integer.parseInt(editTextNumber.getText().toString());
        String[] countries={"None","AT","AR","BR","CA","CN","HR","CZ","EG","FR","DE","GR","IT","JP","MX","ME","NL","PL","PT","RU","ES","UA","UK","US"};

        String country=countries[id];

        String query="";
        if (country=="None") {
            query = String.format("https://api.weatherbit.io/v2.0/forecast/daily?city=%s&key=4e2d6343c40d4652b80a70190ff5ac87", city);
        } else {
            query = String.format("https://api.weatherbit.io/v2.0/forecast/daily?city=%s&country=%s&key=4e2d6343c40d4652b80a70190ff5ac87", city,country);
        }
        weatherDays=new ArrayList<>();
        DownloadTask task = new DownloadTask(this);
        task.execute(query);






    } else {
        Toast.makeText(MainActivity.this, "All fields should be filled correctly", Toast.LENGTH_SHORT).show();
    }
}
public static class DownloadTask extends AsyncTask<String, Void, String> {
        private Context context;
        public DownloadTask(Context context) {
            this.context=context;
        }
    @Override
    protected String doInBackground(String... strings) {
        StringBuilder result = new StringBuilder();
        URL url = null;
        HttpURLConnection urlConnection = null;
        try {
            url = new URL(strings[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            BufferedReader breader = new BufferedReader(reader);
            String line = breader.readLine();
            while (line != null) {
                result.append(line);
                line = breader.readLine();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return result.toString();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        try {
            StringBuilder builder=new StringBuilder();
            JSONObject jsonObject = new JSONObject(s);
            JSONArray jsonArray=jsonObject.getJSONArray("data");
            for(int i=0;i<NumberOfDays;i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                String date=object.getString("datetime");
                String mintemp = object.getString("min_temp");
                String maxtemp=object.getString("max_temp");
                String descr=object.getJSONObject("weather").getString("description");
                String result = date+",."+mintemp+",."+maxtemp+",."+descr;

                WeatherDay weatherDay = new WeatherDay(result);

                weatherDays.add(weatherDay);

            }
            for(WeatherDay weatherDay:weatherDays) {
                builder.append(weatherDay);
            }

            if (weatherDays.isEmpty()) {
                doesExist=false;
            } else {
                doesExist=true;
            }
            if (!doesExist) {

                                Toast.makeText(context, "This city does not exist", Toast.LENGTH_SHORT).show();
            } else {

                Intent intent = new Intent(context, Main2Activity.class);
                intent.putExtra("city", city);
                intent.putExtra("number", number);
                intent.putExtra("country", String.valueOf(id));
                context.startActivity(intent);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

如果不存在城市,则应显示Toast。但是在这种情况下,按此按钮不会发生任何事情。如果我尝试记录某些内容,在这种情况下,什么也不会发生。但是,如果存在城市,则程序运行良好,将启动一个新活动来显示天气预报。另外,如果没有正确填写字段,则将显示Toast。因此,只有在城市不存在的情况下才有麻烦。在这种情况下,日志如下所示:

2020-04-07 12:52:27.881 25832-25832/com.example.kursova W/System.err: org.json.JSONException: End of input at character 0 of 
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:156)
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at com.example.kursova.MainActivity$DownloadTask.onPostExecute(MainActivity.java:123)
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at com.example.kursova.MainActivity$DownloadTask.onPostExecute(MainActivity.java:85)
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at android.os.AsyncTask.finish(AsyncTask.java:660)
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at android.os.AsyncTask.-wrap1(AsyncTask.java)
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at android.os.Looper.loop(Looper.java:154)
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6077)
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
2020-04-07 12:52:27.884 25832-25832/com.example.kursova W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

我不知道如何解决。如果有人可以帮助我,我将感到非常高兴。

java android android-studio android-asynctask
1个回答
0
投票

您的字符串为空,在这种情况下,它将抛出

 org.json.JSONException: End of input at character 0 of 

请检查字符串s是否不为空并且首先不为null。

JSONObject jsonObject = new JSONObject(s);// This line

希望有帮助。.>

© www.soinside.com 2019 - 2024. All rights reserved.