2015-01-15 38 views
0

我正在開發一個Android應用程序,其中我需要使用Web服務。當我檢查SoapUI時(我無法附加我的SoapUI屏幕截圖),我得到了200響應。基本上我需要將查詢參數發送到一個登錄Web服務,這是一個POST請求。如何在Android HttpPost中發送查詢參數

我想在我的Android文件中的下面的代碼片段,但它不工作。我在這裏做錯了什麼?

String loginUrl = "http://my-login-portal.in/LogonServlet"; 

      List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(4); 

      nameValuePair.add(new BasicNameValuePair("page", "/OrderHandlingServlet?api=login")); 
      nameValuePair.add(new BasicNameValuePair("txtDomain", "MyDomain")); 
      nameValuePair.add(new BasicNameValuePair("txtUid", "user1")); 
      nameValuePair.add(new BasicNameValuePair("txtPwd", "pwd1")); 


      httppost = new HttpPost(loginUrl); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));     
      httppost.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded"); 

      response = httpclient.execute(httppost); 

請幫我傢伙..

回答

0

使用此代碼:

 List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(4); 

     nameValuePair.add(new BasicNameValuePair("page", "/OrderHandlingServlet?api=login")); 
     nameValuePair.add(new BasicNameValuePair("txtDomain", "MyDomain")); 
     nameValuePair.add(new BasicNameValuePair("txtUid", "user1")); 
     nameValuePair.add(new BasicNameValuePair("txtPwd", "pwd1")); 

       HttpParams params = new BasicHttpParams(); 
       HttpConnectionParams.setConnectionTimeout(params, INSERTCONNECTIONTIMEOUT); 
       HttpClient client = new DefaultHttpClient(params); 
       HttpPost request = new HttpPost(URL);//put url here 
       request.setEntity(new UrlEncodedFormEntity(nameValuePair)); 
       BasicHttpResponse httpResponse = (BasicHttpResponse) client.execute(request); 
       String data = StreamToString(httpResponse.getEntity().getContent()); 
+0

嗨薩滿,試過以上 - 但沒有運氣。我應該給INSERTCONNECTIONTIMEOUT什麼價值?不知道我在這裏失蹤 – Ramky

+0

INSERTCONNECTIONTIMEOUT必須設置爲int數字,這意味着連接等待的最長時間。同時確保有互聯網許可 – Saman

相關問題