我正在开发一款Android应用,我需要在其中使用网络服务。我在检查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);
答案 0 :(得分: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());