Hallo,
ich möchte einen Java-Server am PC über einen Android-Client ansprechen. Dies klappt eigentlich auch, jedoch habe ich Problem mit der Rückantwort - kann mir hier jemand sagen, wie bzw. wo ich diese am besten anbringe. Hier mein Code des Clients (auf Android). Das Problem ist, dass er beim Empfang der Antwort (Zeile 108) rummeckert, mit "network-Communication on MainThrad Exception" - klar, ich muss es in einen Thread verpacken - aber wie bekomme ich dann die den Response auf den Request, welcher ja dann in einem anderen Thread abgeschickt wird gehandelt, kann ich in einem Thread den Request abschicken und in einem anderen Thread dann die Antwort empfangen - das macht doch auch Probleme.
Schonmal Dank für die Hilfe!
Java
package com.client.cclient;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import com.example.cclient.R;
import com.helper.Helper;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.joystick.*;
import com.joystick.JoyStick.JoyStickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView labelXout;
TextView labelYout;
TextView buttonArming;
TextView textPitch;
TextView textYaw;
TextView textRoll;
TextView textThrottle;
TextView textAux1;
static int pitch = 1111;
static int yaw = 2222;
static int roll = 3333;
static int throttle = 4444;
static int aux1 = 5555;
static int aux2 = 6666;
private Button bottonHoleMotorDaten;
private static String ip = "10.0.2.2";
// private static String ip = "localhost";
// private static String ip = "raspberrypi";
private static int port = 9090;
private Socket socket;
String motordaten = "";
int progressChanged = 0;
JoyStick joy1 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Socket aufbauen
labelXout = (TextView) findViewById(R.id.labelXout);
labelYout = (TextView) findViewById(R.id.labelYout);
bottonHoleMotorDaten = (Button) findViewById(R.id.getMotorDaten);
textPitch = (TextView) findViewById(R.id.textPitch);
textYaw = (TextView) findViewById(R.id.textYaw);
textRoll = (TextView) findViewById(R.id.textRoll);
textThrottle = (TextView) findViewById(R.id.textThrottle);
textAux1 = (TextView) findViewById(R.id.textAux1);
new Thread(new ClientThread()).start();
BufferedReader input;
joy1 = (JoyStick) findViewById(R.id.joy1);
joy1.setPadColor(Color.BLUE);
joy1.setButtonColor(Color.RED);
joy1.setxPut(1000);
joy1.setyPut(0);
joy1.enableStayPutX(true);
joy1.setListener(new JoyStickListener() {
@Override
public void onMove(JoyStick joyStick, double angle, double power) {
}
// wen irgendetwas passiert
public void onHappening(JoyStick joyStick, double angle, double power) {
System.out.println("------onHappening-------");
labelXout.setText(joy1.getPosRelX() + "");
labelYout.setText(joy1.getPosRelY() + "");
roll = (int) joy1.getPosRelX();
throttle = (int) joy1.getPosRelY();
try {
PrintWriter out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println(getMotorDaten());
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(" inputStream: " + input.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
protected void motordatenVerarbeitung(String modaten) {
System.out.println(" motordaten: " + modaten);
pitch = Integer.parseInt(modaten.substring(6, 10));
yaw = Integer.parseInt(modaten.substring(10, 14));
roll = Integer.parseInt(modaten.substring(14, 18));
throttle = Integer.parseInt(modaten.substring(18, 22));
aux1 = Integer.parseInt(modaten.substring(22, 26));
aux2 = Integer.parseInt(modaten.substring(26, 30));
}
public String ID() {
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getDeviceId();
}
String getMotorDaten() {
return "$R<300" + String.format("%04d", pitch) + String.format("%04d", yaw) + String.format("%04d", roll)
+ String.format("%04d", throttle) + String.format("%04d", aux1) + String.format("%04d", aux2);
}
class ClientThread implements Runnable {
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(ip);
socket = new Socket(serverAddr, port);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
Alles anzeigen