我正在尝试使用POST方法将数据从我的Android应用程序发送到servlet但是servlet没有显示任何类型的活动,只是在输出中显示了null。虽然我正在使用GET方法从Android应用程序上的servlet接收响应。 我们尝试在logcat上打印动作。它显示数据已发送但在浏览器上显示为null。 我也收到异常“无效使用单客户端连接管理器:仍然分配连接”
Android代码: -
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);
}
Servlet代码: -
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
答案 0 :(得分:0)
在没有外部工具的情况下调试网络应用程序很困难,当然如果不编译它就很难找到代码中的错误。
为了调试这些应用程序,我使用Android Emulator + WireShark来查看HTTP请求的主体。希望它会有所帮助。
编辑:
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try{
String strResponseSaveGoal =
httpclient.execute(httppost, responseHandler);
Log.d("sendCancelOrderRequest", strResponseSaveGoal);
}catch ( ConnectTimeoutException conEx )
{
handler.sendEmptyMessage(CANCEL_REQUEST_ERROR);
}
catch (ClientProtocolException ex) {
Log.d("sendCancelOrderRequest","");
handler.sendEmptyMessage(CANCEL_REQUEST_ERROR);
}catch (IOException ex) {
Log.d("sendCancelOrderRequest","");
handler.sendEmptyMessage(CANCEL_REQUEST_ERROR);
}