服务器的Java传感器id
我目前正在做一项使用Phidges的作业。我有这段代码,它输出phidgets详细信息,如序列号、设备版本及其值。我可以让它把序列号打印到控制台上服务器的Java传感器id,java,Java,我目前正在做一项使用Phidges的作业。我有这段代码,它输出phidgets详细信息,如序列号、设备版本及其值。我可以让它把序列号打印到控制台上 System.out.println("Serial Number " + phid.getSerialNumber()); 如何将其添加到服务器上的字符串fullURL中 String fullURL = sensorServerURL + "?sensorname=rotationsensor&sensorvalue="+sensor
System.out.println("Serial Number " + phid.getSerialNumber());
如何将其添加到服务器上的字符串fullURL中
String fullURL = sensorServerURL + "?sensorname=rotationsensor&sensorvalue="+sensorValue;
任何帮助都将不胜感激,我的任务截止日期是今晚11点,这个问题困扰了我一段时间,似乎无法解决:/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.phidgets.InterfaceKitPhidget;
import com.phidgets.PhidgetException;
import com.phidgets.event.AttachEvent;
import com.phidgets.event.AttachListener;
import com.phidgets.event.DetachEvent;
import com.phidgets.event.DetachListener;
import com.phidgets.event.ErrorEvent;
import com.phidgets.event.ErrorListener;
import com.phidgets.event.InputChangeEvent;
import com.phidgets.event.InputChangeListener;
import com.phidgets.event.OutputChangeEvent;
import com.phidgets.event.OutputChangeListener;
import com.phidgets.event.SensorChangeEvent;
import com.phidgets.event.SensorChangeListener;
public class SensorToServer implements SensorChangeListener, InputChangeListener,
AttachListener, DetachListener, ErrorListener, OutputChangeListener {
public static int LED_PORT=7;
public static int INPUT_PORT=3;
public static int ANALOG_PORT=7;
public static String sensorServerURL = "http://localhost:8080/SensorServer/sensorToDB";
public static void main(String[] args) throws PhidgetException {
new SensorToServer();
}
public SensorToServer() throws PhidgetException {
InterfaceKitPhidget phid = new InterfaceKitPhidget();
try {
phid.addAttachListener(this);
phid.addDetachListener(this);
phid.addSensorChangeListener(this);
phid.addInputChangeListener(this);
//phid.addOutputChangeListener(this);
phid.openAny();
phid.waitForAttachment();
System.out.println(phid.getDeviceType());
System.out.println("Serial Number " + phid.getSerialNumber());
System.out.println("Device Version " + phid.getDeviceVersion());
System.out.println("Looping...\n");
boolean led = true;
boolean dummy = true;
for (; dummy;) {
phid.setOutputState(LED_PORT, led);
if (!phid.getInputState(INPUT_PORT))
led = !led;
try {
// changing the output too often seems to mess up the sensor
// box
// Thread.sleep(100 + phid.getSensorValue(7));
Thread.sleep(1000);
// Thread.sleep(1000);
} catch (Throwable t) {
t.printStackTrace();
}
}
} finally {
phid.close();
System.out.println("Closed and exiting...");
}
}
public void sensorChanged(SensorChangeEvent arg0) {
// System.out.println(arg0);
int sensorValue = arg0.getValue();
System.out.println("Slider value is now "+sensorValue);
String sendResult = sendToServer(sensorValue);
try {
System.out.println("Sleeping.... 1 sec");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Sent to server, result: "+sendResult);
}
public void inputChanged(InputChangeEvent arg0) {
System.out.println(arg0);
}
public void attached(AttachEvent arg0) {
System.out.println(arg0);
}
public void detached(DetachEvent arg0) {
System.out.println(arg0);
}
public void error(ErrorEvent arg0) {
System.out.println(arg0);
}
public void outputChanged(OutputChangeEvent arg0) {
System.out.println(arg0);
}
public String sendToServer(int sensorValue){
URL url;
HttpURLConnection conn;
BufferedReader rd;
String fullURL = sensorServerURL + "?sensorname=rotationsensor&sensorvalue="+sensorValue;
System.out.println("Sending data to: "+fullURL);
String line;
String result = "";
try {
url = new URL(fullURL);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
return "done : "+result;
// return result;
}
}
您不能将序列号保存在变量上,并使用另一个参数构建
fullUrl
fullURL=sensorServerURL+“?sensorname=rotationsensor&sensorvalue=“+sensorvalue+”&serialNumber=“+serialNumberVariable代码>您需要将其作为参数添加到url,您已经有了类似的东西&sensorvalue=“+sensorvalue”。您需要实例&serialNumber=phid.getSerialNumber()添加到url。不幸的是,要正确处理它,您需要对服务器端进行一些更改。您不能将serialNumber保存在变量上,并使用另一个参数构建fullUrl
?fullUrl=sensorServerURL+“?sensorname=rotationsensor&sensorvalue=“+sensorvalue+”&serialNumber=“+序列号可变代码>您需要将其作为参数添加到url,您已经有了类似的东西&sensorvalue=“+sensorvalue。您需要将实例&serialNumber=phid.getSerialNumber()添加到url。不幸的是,要正确处理它,您需要对服务器端进行一些更改。