如何在 Android HttpPost 中发送查询参数

How to send Query Parameters in Android HttpPost

我正在开发一个 Android 应用程序,我需要在其中使用 Web 服务。当我签入 SoapUI 时收到 200 响应(我无法附上我的 SoapUI 屏幕截图)。基本上我需要将查询参数发送到登录 Web 服务,这是一个 POST 请求。

我正在我的 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);

使用此代码:

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());