我是处理servlet的新手,并试图从Android应用程序向我的Servlet发送数据,但是不知道在我的servlet中读取它的确切方法? 在android我已经使用这些来发送数据:
URL url = new URL("http://example.appspot.com/testservlet");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("firstParam", "Rahul")
.appendQueryParameter("secondParam", "Anil")
.appendQueryParameter("thirdParam", "Rohan");
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
}catch(Exception e){
Exception exep= e;
Log.d("Test",exep+"");
return false;
}
Log.d("Test","Succesfully")
return true;
在servlet之后我写道:
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain");
resp.setContentType("text/plain");
String request=req.getParameter("firstParam");
resp.getWriter().println("firstParam ::::::::::"+request);
}
这是否正确如果没有请提供一些例子,比如发送一个字符串并在servlet中获取它? Thanx提前
答案 0 :(得分:-1)
此示例可能对您有所帮助:
private Document sendRequest(String requestXML) throws Exception {
/**
// Open HTTP connection with TransitServlet
URLConnection tc = null;
try {
tc = servletURL.openConnection();
} catch (IOException ioe) {
throw(ioe);
}
tc.setDoOutput(true);
tc.setDoInput(true);
// Write request to HTTP request body
OutputStreamWriter out = new OutputStreamWriter(tc.getOutputStream());
out.write(requestXML);
out.flush();
*/
// Read response into XML document object
DocumentBuilderFactory docBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
Document xmlDoc = null;
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
throw(pce);
}
initializeDataField();
InputStream tcIn = null;
try {
tcIn = submitRequest(requestXML);
xmlDoc = docBuilder.parse(tcIn);
} catch (IOException ioe) {
throw(ioe);
} catch (SAXException saxe) {
throw(saxe);
}
//out.close();
tcIn.close();
// Return XML document object for interpretation
return(xmlDoc);
}
答案 1 :(得分:-2)
我找到了一个例子,它对你有用:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Non-blocking-read-war</title>");
out.println("</head>");
out.println("<body>");
String urlPath = "http://"
+ request.getServerName()
+ ":" + request.getLocalPort() //default http port is 8080
+ request.getContextPath()
+ "/ServerTest";
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setChunkedStreamingMode(2);
conn.setRequestProperty("Content-Type", "text/plain");
conn.connect();
try {
output = conn.getOutputStream();
// Sending the first part of data to server
String firstPart = "Hello";
out.println("Sending to server: " + firstPart + "</br>");
out.flush();
writeData(output, firstPart);
Thread.sleep(2000);
// Sending the second part of data to server
String secondPart = "World";
out.println("Sending to server: " + secondPart + "</br></br>");
out.flush();
writeData(output, secondPart);
// Getting the echo data from server
input = conn.getInputStream();
printEchoData(out, input);
out.println("Please check server log for detail");
out.flush();
} catch (IOException ioException) {
Logger.getLogger(ReadListenerImpl.class.getName()).log(Level.SEVERE,
"Please check the connection or url path", ioException);
} catch (InterruptedException interruptedException) {
Logger.getLogger(ReadListenerImpl.class.getName()).log(Level.SEVERE,
"Thread sleeping error", interruptedException);
} finally {
if (input != null) {
try {
input.close();
} catch (Exception ex) {
}
}
if (output != null) {
try {
output.close();
} catch (Exception ex) {
}
}
}
out.println("</body>");
out.println("</html>");
}