这是我的代码
class BackgroundTask extends AsyncTask<Void, Void, String> {
String target;
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
target = "http://sanghoboy135.cafe24.com/courseList.php?courseUniversity=" + URLEncoder.encode(courseUniversity, "UTF-8") +
"&courseYear=" + URLEncoder.encode(yearSpinner.getSelectedItem().toString().substring(0, 4), "UTF-8") + "&courseTerm=" + URLEncoder.encode(termSpinner.getSelectedItem().toString(), "UTF-8") +
"&courseArea=" + URLEncoder.encode(areaSpinner.getSelectedItem().toString(), "UTF-8") + "&courseMajor=" + URLEncoder.encode(majorSpinner.getSelectedItem().toString(), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL(target);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//if (connection != null) {
// connection.setRequestMethod("GET");
// connection.getResponseCode();
//}
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String temp = "";
StringBuilder builder = new StringBuilder();
while ((temp = reader.readLine()) != null) {
builder.append(temp + "\n");
Log.d("CourseFragment1", temp);
}
reader.close();
inputStream.close();
connection.disconnect();
return builder.toString().trim();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("CourseFragment2", s);
try {
String prefix = "null";
if(s.startsWith(prefix)){
s = s.substring(prefix.length(), s.length());
}
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("response");
int count = 0;
int courseID;
String courseUniversity;
int courseYear;
String courseTerm;
String courseArea;
String courseMajor;
String courseGrade;
String courseTitle;
int courseCredit;
int courseDivide;
int coursePersonnel;
String courseProfessor;
String courseTime;
String courseRoom;
courseList = new ArrayList<Course>();
while (count < jsonArray.length()) {
JSONObject object = jsonArray.getJSONObject(count);
courseID = object.getInt("courseID");
courseUniversity = object.getString("courseUniversity");
courseYear = object.getInt("courseYear");
courseTerm = object.getString("courseTerm");
courseArea = object.getString("courseArea");
courseMajor = object.getString("courseMajor");
courseGrade = object.getString("courseGrade");
courseTitle = object.getString("courseTitle");
courseCredit = object.getInt("courseCredit");
courseDivide = object.getInt("courseDivide");
coursePersonnel = object.getInt("coursePersonnel");
courseProfessor = object.getString("courseProfessor");
courseTime = object.getString("courseTime");
courseRoom = object.getString("courseRoom");
Course course = new Course(courseID, courseUniversity, courseYear, courseTerm, courseArea, courseMajor, courseGrade, courseTitle, courseCredit, courseDivide, coursePersonnel, courseProfessor, courseTime, courseRoom);
courseList.add(course);
count++;
}
//Course course = new Course(1, "학부", 2016, "1학기", "교양및기타", "군사학", "3", "군사학", 2, 1, 0, "허진회", "", "");
//courseList.add(course);
adapter = new CourseAdapter(getActivity(), courseList);
courseListView.setAdapter(adapter);
/*if(count == 0){
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
dialog.setMessage("조회된 강의가 없습니다.")
.setPositiveButton("확인", null)
.create();
dialog.show();
}
*/
//adapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Logcat
D/CourseFragment1: NULL
NULL
D/CourseFragment1: {"response":[{"courseID":"1","courseUniversity":"학부","courseYear":"2016","courseTerm":"2학기","courseArea":"교양및기타","courseMajor":"군사학","courseGrade":"3","courseTitle":"군사학Ⅱ","courseCredit":"2","courseDivide":"1","coursePersonnel":"0","courseProfessor":"허진회","courseTime":"","courseRoom":""},{"courseID":"2","courseUniversity":"학부","courseYear":"2016","courseTerm":"2학기","courseArea":"교양및기타","courseMajor":"군사학","courseGrade":"4","courseTitle":"군사학Ⅳ","courseCredit":"2","courseDivide":"1","coursePersonnel":"0","courseProfessor":"허진회","courseTime":"","courseRoom":""}]}
错误
W / System.err:org.json.JSONException:类型的值null org.json.JSONObject $ 1无法转换为JSONObject
我真的不知道为什么json get返回null和JSONArray。代码中是否有任何问题?还是应该只获取没有空值的JSONArray?