您好我在尝试从我的服务器获取数组时出现问题。我会尝试尽可能具体。我希望这篇文章不会太长。
public static Appointment[] getAppointments(DateTime date, String titel, String note, Date startDate, Date endDate, String location) {
Gson gson = new GsonBuilder().create();
Appointment[] appointments = new Appointment[1];
CalendarDate calendarDate;
appointments.setTitle(titel);
appointments.setNote(note);
appointments.setStartDate(startDate);
appointments.setEndDate(endDate);
appointments.setLocation(location);
String gsonString = gson.toJson(date);
String result = GetCalendar(gsonString);
appointments = (Appointment[])gson.fromJson(result, Appointment[].class);
return appointments;
}
private static String GetCalendar(String jsonInput){
String result = "Error";
try {
Socket clientSocket = new Socket("localhost", 8888);
DataOutputStream outToServer = new DataOutputStream(
clientSocket.getOutputStream());
byte[] input = jsonInput.getBytes();
byte key = (byte) 3.1470;
byte[] encrypted = input;
for (int i = 0; i < encrypted.length; i++)
encrypted[i] = (byte) (encrypted[i] ^ key);
System.out.println(encrypted);
outToServer.write(encrypted);
outToServer.flush();
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
result = inFromServer.readLine();
clientSocket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
它应该从此类接收数据,该类位于serverproject上。
public class Events {
ArrayList<Calendar> events = new ArrayList<Calendar>();
public ArrayList<Calendar> getEvents() {
QueryBuilder qb = new QueryBuilder();
try {
ResultSet rs = qb.selectFrom("calendar").all().ExecuteQuery();
while (rs.next())
{
//String values from SQL database (must be created)
int calendarID = rs.getInt("Calendarid");
int createdby = rs.getInt("userid");
Date startDate = rs.getDate("start");
Date endDate = rs.getTime("end");
String title = rs.getString("Title");
String note = rs.getString("Note");
String location = rs.getString("Location");
int stringCalendarID = Integer.valueOf(calendarID);
int stringCreatedby = Integer.valueOf(createdby);
String stringTitle = String.valueOf(title);
String stringNote = String.valueOf(note);
String stringLocation = String.valueOf(location);
//System.out.println(String.valueOf(startDate.getTime()));
events.add(new Calendar(stringCalendarID, stringCreatedby, stringTitle, stringNote, stringLocation));
}
} catch (SQLException e) {
e.printStackTrace();
}
return events;
}
public void setEvents(ArrayList<Calendar> calendar) {
this.events = calendar;
}
但是我在所有约会中都会收到此错误
appointments.setTitle(titel);
appointments.setNote(note);
appointments.setStartDate(startDate);
appointments.setEndDate(endDate);
appointments.setLocation(location);
无法在数组类型Appointment []
上调用setTitle(String) 我在做错了什么? 我希望你能帮忙:)。答案 0 :(得分:0)
数组没有setTitle
方法,Appointment
做(我假设)。
也许您正在寻找类似的东西:
Appointment appointment = new Appointment();
appointment.setTitle(titlel);
//etc.
Appointment[] appointments = new Appointment[1];
appointments[0] = appointment;
答案 1 :(得分:0)
Appointment []创建一个全新的对象类型:一个约会对象数组。它不再只是一个约会对象,因此不会继承setTitle成员。
你必须从数组中获取一个约会对象,然后在其上调用setTitle函数:
例如
Appointment a = appointments[who];
a.setTitle("whatever");
甚至更简单
(appointments[who]).setTitle("whatever");
&#34;谁&#34;是一个整数,表示两个示例
中约会数组中项的索引