0

我正在尝试使用 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
4

1 回答 1

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);
        }
于 2013-07-30T05:40:21.100 回答