如何将HttpGet转换为HttpPost?

时间:2011-08-30 05:38:38

标签: android http-post http-get

我正在使用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时没问题。怎么解决?请帮忙

1 个答案:

答案 0 :(得分:2)

以下是代码,这对您有帮助。

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