【发布时间】:2011-11-06 12:49:57
【问题描述】:
我正在使用 HttpGet 方法从我的 Android 应用程序的 Web 服务中检索数据。谁能告诉我如何将下面给出的代码转换为 HttpPost 方法?
String url = URLEditor.encode("http://"+Constants.strURL+"Orders.asmx/CheckWebConnection?TechCode="+username+"&TechPIN="+password);
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if(entity == null) return false;
is = entity.getContent();
提前谢谢...
谢谢你帮助我..
我尝试了上面给出的代码。但我将 Document 对象设为 NULL。这是代码
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://"+Constants.strURL+"Orders.asmx/CheckWebConnection");
List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
nvpList.add(new BasicNameValuePair("TechCode", techcode));
nvpList.add(new BasicNameValuePair("TechPIN", techpin));
httpPost.setEntity(new UrlEncodedFormEntity(nvpList));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
我将 doc 设为 NULL。当我使用 HttpGet 时没有问题。如何解决?请帮忙
【问题讨论】:
-
如果 Web 服务不响应 POST,那么您将不走运。从 GET 到 POST 并没有真正的“切换”;它们是不同的http方法。你为什么要这样做?
标签: android http-post http-get