我正在开发Android应用程序,我在从数据库获取数据并使用JSONObject和HttpGet将其显示到我的应用程序时遇到问题。请帮我解决我的错误。
错误:
Value 1 of type java.lang.Integer cannot be converted to JSONArray
ViewPatient类:
public class ViewPatient extends Activity {
public static ExpandListAdapter ExpAdapter;
HttpClient client;
private ArrayList<Group> expListItems;
public ExpandableListView expandableListView;
String user_name, password;
String URL = OpdLogin.URL;
String unm, pass;
String group_names[] = new String[100];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_patient);
Bundle bundle = getIntent().getExtras();
unm = bundle.getString("user_name");
pass = bundle.getString("password");
expandableListView = (ExpandableListView) findViewById(R.id.exp_list);
expListItems = setStandardGroups();
ExpAdapter = new ExpandListAdapter(ViewPatient.this, expListItems);
}
public ArrayList<Group> setStandardGroups() {
// TODO Auto-generated method stub
client = new DefaultHttpClient();
String url = OpdLogin.URL;
ArrayList<Group> list = new ArrayList<Group>();
HttpGet get = new HttpGet(url + "patientdetails?");
try {
get.setHeader("Accept", "application/json");
get.setHeader("Content-type", "application/json ");
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
int status = response.getStatusLine().getStatusCode();
String result = EntityUtils.toString(entity);
JSONArray jarray = new JSONArray(result);
ArrayList<Child> ch_list;
for (int i = 0; i < jarray.length(); i++) {
JSONObject jobj = jarray.getJSONObject(i);
String jkey = jobj.getString("key");
group_names[i] = jkey;
String jvalue = jobj.getString("value");
JSONArray JArray = new JSONArray(jvalue);
Group gru = new Group();
gru.setFname(jkey);
ch_list = new ArrayList<Child>();
for (int j = 0; j < JArray.length(); j++) {
Child ch = new Child();
JSONObject Jobj = JArray.getJSONObject(j);
String lname = Jobj.getString("last _name");
String email = Jobj.getString("email");
String state = Jobj.getString("state");
String city = Jobj.getString("city");
int mob = Jobj.getInt("mobileno");
int age = Jobj.getInt("age");
String gender = Jobj.getString("gender");
ch.setLname(lname);
ch.setEmail(email);
ch.setState(state);
ch.setCity(city);
ch.setMobileno(mob);
ch.setAge(age);
ch.setGender(gender);
ch_list.add(ch);
}
gru.setItems(ch_list);
list.add((Group) gru);
}
} catch (Exception e) {
// TODO: handle exception
Log.d("Error in Http", e.getMessage());
}
return list;
}
适配器类:
public class ExpandListAdapter extends BaseExpandableListAdapter {
private Context context;
public static ArrayList<Group> expListItems;
public static ArrayList<Child> childList;
Group g = new Group();
Child c = new Child();
ViewPatient viewPatient = new ViewPatient();
public ExpandListAdapter(Context context, ArrayList<Group> expListItems) {
// TODO Auto-generated constructor stub
this.context = context;
this.expListItems = expListItems;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return expListItems.size();
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
ArrayList<Child> chList = expListItems.get(groupPosition).getItems();
return chList.size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return expListItems.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
childList = expListItems.get(groupPosition).getItems();
return childList.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Group group = (Group) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.group_name);
tv.setText(group.getFname());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Child child = (Child) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater inflaterInfl = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inflaterInfl.inflate(R.layout.view_patient_details,
null);
}
TextView list = (TextView) convertView.findViewById(R.id.txtName);
list.setText(child.getLname().toString());
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
小组课程:
public class Group {
private String fname;
private String disease;
private int dob;
private ArrayList<Child> items;
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getDisease() {
return disease;
}
public void setDisease(String disease) {
this.disease = disease;
}
public int getDob() {
return dob;
}
public void setDob(int dob) {
this.dob = dob;
}
public ArrayList<Child> getItems() {
return items;
}
public void setItems(ArrayList<Child> items) {
this.items = items;
}
儿童班:
public class Child {
private String lname;
private String email;
private String state;
private String city;
private int mobileno;
private int age;
private String gender;
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getMobileno() {
return mobileno;
}
public void setMobileno(int mobileno) {
this.mobileno = mobileno;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}