如何将 HttpGet 转换为 HttpPost?

发布于 2024-12-01 22:36:23 字数 1384 浏览 0 评论 0原文

我正在使用 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。这是我得到的 doc 为 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);

。我使用 HttpGet 时没有问题。怎么解决呢?请帮忙

I'm using HttpGet method for retrieving data from a web service from my Android app. Can anyone tell me how to convert the below given code to HttpPost method?

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

Thanks in advance...


thanks for helping me..

I tried with the code given above. But I get Document object as NULL. This's the code

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

I get doc as NULL. No issues when I use HttpGet. How can it be solved? Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

口干舌燥 2024-12-08 22:36:23

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

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

Here is the code, this will help you.

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());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文