这是Java代码使用套接字从C服务器接收数据并将数据存储在Google Data Store中并在Google App Engine上部署应用程序。
package pack.exp;
@SuppressWarnings("serial")
public class CToJavaToCloudServlet extends HttpServlet
{
static List<String> obj1;
public static void main(String[] args) throws UnknownHostException, IOException
{
Socket s = new Socket("10.9.11.55", 6870);
InputStream stream = s.getInputStream();
byte[] data = new byte[13];
int read;
String can_Id= null;
while((read = stream.read(data)) != -1)
{
can_Id= String.format("%02X%02X%02X%02X", data[0], data[1], data[2],
data[3]);
String can_Data= String.format("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11]);
obj1= new ArrayList<String>();
obj1.add(can_Data.substring(0, 2));
obj1.add(can_Data.substring(3, 5));
obj1.add(can_Data.substring(6, 8));
obj1.add(can_Data.substring(9, 11));
obj1.add(can_Data.substring(12, 14));
obj1.add(can_Data.substring(15, 17));
obj1.add(can_Data.substring(18, 20));
obj1.add(can_Data.substring(21, 23));
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
IOException
{
Key k1 = KeyFactory.createKey("C","0C F0 0400");
String statusEEC1= obj1.get(0);
String driversDemandEnginePercentTorque= obj1.get(1);
String actualEnginePercentTorque= obj1.get(2);
String engineSpeed= obj1.get(3) + obj1.get(4);
String sourceAddressOfControllingDeviceForEngineControl= obj1.get(5);
String engineStarterMode= obj1.get(6);
String engineDemandPercentTorque= obj1.get(7);
Entity can1 = new Entity(k1);
can1.setProperty("Status EEC", statusEEC1);
can1.setProperty("Drivers Demand Engine Percent Torque",
driversDemandEnginePercentTorque);
can1.setProperty("Actual Engine Percent Torque",
actualEnginePercentTorque);
can1.setProperty("Engine Speed", engineSpeed);
can1.setProperty("Source Address Of Controlling Device For Engine Control",
sourceAddressOfControllingDeviceForEngineControl);
can1.setProperty("Engine Starter Mode", engineStarterMode);
can1.setProperty("Engine Demand Percent Torque",
engineDemandPercntTorque);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
datastore.put(can1);
Entity can11 = null;
try
{
can11= datastore.get(k1);
}
catch (EntityNotFoundException e)
{
e.printStackTrace();
}
String statusEEC11= (String) can11.getProperty("Status EEC");
String driversDemandEnginePercentTorque1= (String)
can11.getProperty("Drivers Demand Engine Percent Torque");
String actualEnginePercentTorque1= (String) can11.getProperty("Actual
Engine Percent Torque");
String engineSpeed1= (String) can11.getProperty("Engine Speed");
String sourceAddressOfControllingDeviceForEngineControl1= (String)
can11.getProperty("Source Address Of Controlling Device For Engine
Control");
String engineStarterMode1= (String) can11.getProperty("Engine Starter
Mode");
String engineDemandPercentTorque1= (String) can11.getProperty("Engine
Demand Percent Torque");
resp.setContentType("text/plain");
resp.getWriter().println("Can id 0C F0 0400------>");
resp.getWriter().println("");
resp.getWriter().println("Status EEC--- " + statusEEC11);
resp.getWriter().println("Drivers Demand Engine Percent Torque---" +
driversDemandEnginePercentTorque1);
resp.getWriter().println("Actual Engine Percent Torque---" +
actualEnginePercentTorque1);
resp.getWriter().println("Engine Speed---" + engineSpeed);
resp.getWriter().println("Source Address Of Controlling Device For Engine
Control---" + sourceAddressOfControllingDeviceForEngineControl1);
resp.getWriter().println("Engine Starter Mode---" + engineStarterMode1);
resp.getWriter().println("Engine Demand Percent Torque---" +
engineDemandPercentTorque1);
resp.getWriter().println("");
resp.getWriter().println("");
resp.getWriter().println("");
resp.getWriter().println("");
}
}
在将应用部署到Google云引擎时,我收到了错误消息。 该网站无法显示该页面 HTTP 500 最可能的原因: •网站正在维护中。 •网站出现编程错误。
任何人都可以帮我解决这个问题。
答案 0 :(得分:1)
javax.servlet
和javax.servlet.http
包提供了用于编写servlet的接口和类。所有servlet都必须实现Servlet接口,该接口定义了生命周期方法。实现通用服务时,可以使用或扩展随Java Servlet API提供的GenericServlet类。 HttpServlet类提供了用于处理特定于HTTP的服务的方法,例如doGet
和doPost
。
由于您要实施HttpServlet
界面,因此必须实施doGet
和/或doPost
。
Servlet init方法允许servlet在处理请求之前执行一次性初始化。实现init方法时的一个常见错误是以这种形式:
因此根本不会调用您的main
方法。将代码从main移动到init
。您的代码的另一个问题是您正在打开期望数据的套接字。这应该在doGet
和/或doPost
方法中完成,因为它们负责接收数据。