我正在尝试使用 POST 方法将数据从我的 android 应用程序发送到 servlet,但 servlet 不显示任何类型的活动,只是 null 已在输出中显示。尽管我正在使用 GET 方法在 android 应用程序上接收来自 servlet 的响应。我们尝试在 logcat 上打印操作。它显示数据已发送,但在浏览器上显示为 null。我也收到异常“单个客户端连接管理器的使用无效:连接仍然分配”
安卓代码:-
String url = "http://10.0.2.2:8084/AndroidApp/AndroidServlet";
DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler<String> res = new BasicResponseHandler();
HttpPost httpPost = new HttpPost(url);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("Name", name));
nameValuePairs.add(new BasicNameValuePair("Father", father));
nameValuePairs.add(new BasicNameValuePair("Gender", gender));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
String response = httpClient.execute(httpPost, res);
}
小服务程序代码:-
public class AndroidServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ClassNotFoundException, SQLException {
String Name=request.getParameter("Name");
String Father=request.getParameter("Father");
String Gender=request.getParameter("Gender");
try (PrintWriter out = response.getWriter()) {
out.println("Hello Android !!!!");
out.println( Name + " " + Father + " " + Gender + " ");
}
}
浏览器上的输出:-
Hello Android !!!!
null null null