【问题标题】:How to convert HttpGet to HttpPost?如何将 HttpGet 转换为 HttpPost?
【发布时间】: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


【解决方案1】:

这是代码,这将对您有所帮助。

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("TechCode",username));
    nameValuePairs.add(new BasicNameValuePair("TechPIN",password));
try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://"+Constants.strURL+"Orders.asmx/CheckWebConnection");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
    }catch(Exception e){
    Log.e("log_tag", "Error in http connection"+e.toString());
    }

【讨论】:

  • 我的网络服务似乎不支持 HttpPost。谁能告诉我如何使用 SOAP 做同样的事情?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
相关资源
最近更新 更多