Hallo,
Ich habe zwei Klassen ConnectActivity und ConrollerActivity. In ConnectActivity wird ein Object vom typ Connection (auch eine eigene klasse) erstellt. Ich muss irgendwie dieses Objekt ControllerActivity übergeben. ControllerActivity rufe ich so auf:
Intent intent = new Intent(this, ControllerActivity.class);
startActivity(intent);
Ich habe es schon mit Gson probiert, aber ich bekomme da immer ein Stackoverflow. Ich glaube weil Connection AsyncTask extended.
Hier etwas Code:
Gson:
Connection c = new Connection();
Gson gS = new Gson();
String target = gS.toJson(c); <-- gibt stackoverflowError
Connection:
public class Connection extends AsyncTask<Void,Void,Void> {
Socket socket = null;
DataOutputStream DOS = null;
DataInputStream DIS = null;
boolean connected = false;
String message = "";
void write(String message) {
this.message = message;
}
@Override
protected Void doInBackground(Void... voids) {
try {
socket = new Socket("192.168.2.105",5000);
DOS = new DataOutputStream(socket.getOutputStream());
DIS = new DataInputStream(socket.getInputStream());
DOS.writeByte(1);
System.out.println(DIS.readUTF());
connected = true;
if(connected) {
do {
if(!(message.isEmpty())) {
DOS.writeUTF(message);
System.out.println(message);
if(!message.contains("disconnect")) message = "";
}
} while(!message.contains("disconnect"));
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//openConnectActivity();
DOS.flush();
//DOS.close();
//DIS.close();
//socket.close(); //TODO ON APP CLOSE
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Wie kann ich Gson verwenden oder gibt es noch eine andere Möglichkeit bin echt am verzweifeln -.-
Ich habe zwei Klassen ConnectActivity und ConrollerActivity. In ConnectActivity wird ein Object vom typ Connection (auch eine eigene klasse) erstellt. Ich muss irgendwie dieses Objekt ControllerActivity übergeben. ControllerActivity rufe ich so auf:
Intent intent = new Intent(this, ControllerActivity.class);
startActivity(intent);
Ich habe es schon mit Gson probiert, aber ich bekomme da immer ein Stackoverflow. Ich glaube weil Connection AsyncTask extended.
Hier etwas Code:
Gson:
Connection c = new Connection();
Gson gS = new Gson();
String target = gS.toJson(c); <-- gibt stackoverflowError
Connection:
public class Connection extends AsyncTask<Void,Void,Void> {
Socket socket = null;
DataOutputStream DOS = null;
DataInputStream DIS = null;
boolean connected = false;
String message = "";
void write(String message) {
this.message = message;
}
@Override
protected Void doInBackground(Void... voids) {
try {
socket = new Socket("192.168.2.105",5000);
DOS = new DataOutputStream(socket.getOutputStream());
DIS = new DataInputStream(socket.getInputStream());
DOS.writeByte(1);
System.out.println(DIS.readUTF());
connected = true;
if(connected) {
do {
if(!(message.isEmpty())) {
DOS.writeUTF(message);
System.out.println(message);
if(!message.contains("disconnect")) message = "";
}
} while(!message.contains("disconnect"));
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//openConnectActivity();
DOS.flush();
//DOS.close();
//DIS.close();
//socket.close(); //TODO ON APP CLOSE
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Wie kann ich Gson verwenden oder gibt es noch eine andere Möglichkeit bin echt am verzweifeln -.-