2013-07-30 27 views
0

我想要使用POST方法將數據從我的android應用程序發送到servlet,但servlet不顯示任何種類的null活動只顯示在輸出中。儘管我正在使用GET方法從android應用程序的servlet接收響應。 我們嘗試在logcat上打印操作。它顯示數據已發送,但在瀏覽器上顯示爲空。 我也收到異常 「無效使用單個客戶端連接管理器:仍然分配的連接」在Android上接收servlet上的數據?

的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

很難在沒有外部工具的情況下調試網絡應用程序當然,如果沒有編譯它,很難在代碼中發現錯誤。
爲了調試這些應用程序,我使用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); 
     } 
+0

嗯,你能告訴如果在我的方法有任何問題或方法是正確的,問題是其他地方 – Saadhassan

+0

我不是Nastradamus)我可以在我的大腦沒有調試代碼)其實我沒有看到你的代碼中有任何錯誤。但我應該建議你去捕捉其他異常。看看我上面修改的答案 –

+0

我已經完成了,但沒有出現異常。 – Saadhassan