首页 > 解决方案 > JSONException: courseDivide 没有值

问题描述

刚尝试运行,我收到此错误:

JSONException: courseDivide StatisticsFragment$ByEntire.onPostExecute(StatisticsFragment.java:225) StatisticsFragment$ByEntire.onPostExecute(StatisticsFragment.java:153) 没有值

我是 Java 的初学者,我似乎无法弄清楚为什么 JSONException 没有价值。

编码:

class ByEntire extends AsyncTask<Void, Void, String> {

    String target;
    ProgressDialog dialog = new ProgressDialog(getActivity());  

    @Override
    protected void onPreExecute(){

        try{
            target = "http://wook1150.cafe24.com/ByEntire.php";
            dialog.setMessage("로딩중");   
            dialog.show();                
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    @Override
    protected String doInBackground(Void... voids) {
        try {
            URL url = new URL(target);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String temp;
            StringBuilder stringBuilder = new StringBuilder();
            while ((temp = bufferedReader.readLine()) !=null)
            {
                stringBuilder.append(temp + "\n");
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public  void onProgressUpdate(Void... values){
        super.onProgressUpdate();
    }

    @Override
    public  void onPostExecute(String result){
        try{
            JSONObject jsonObject = new JSONObject(result);
            JSONArray jsonArray = jsonObject.getJSONArray("response");
            int count = 0;
            int courseID;
            String courseGrade;
            String courseTitle;
            String courseProfessor;
            int courseCredit;
            int courseDivide;     //////Error here//////
            int coursePersonnel;
            String courseTime;

            while(count < jsonArray.length()){

                JSONObject object = jsonArray.getJSONObject(count);
                courseID = object.getInt("courseID");
                courseGrade = object.getString("courseGrade");
                courseTitle = object.getString("courseTitle");
                courseProfessor = object.getString("courseProfessor");
                courseCredit = object.getInt("courseCredit");
                courseDivide = object.getInt("courseDivide)");
                coursePersonnel = object.getInt("coursePersonnel)");
                courseTime = object.getString("courseTime)");
                rankList.add(new Course(courseID, courseGrade, courseTitle, courseCredit, courseDivide, coursePersonnel,courseTime, courseProfessor));
                count++;
                dialog.dismiss();   
            }
            rankListAdapter.notifyDataSetChanged();
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

标签: javaandroid-studio

解决方案


可能是您的 JSON 没有 courseDivide 或输入错误,删除 ')'

courseDivide = object.getInt("courseDivide");
coursePersonnel = object.getInt("coursePersonnel");
courseTime = object.getString("courseTime");

推荐阅读