2011-08-30 72 views
0

我正在使用HttpGet方法从我的Android应用程序中从Web服务中检索数据。任何人都可以告诉我如何将下面给出的代码转换为HttpPost方法?如何将HttpGet转换为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时没有问题。它如何解决?请帮忙

+0

如果Web服务没有对​​POST做出响应,那么您的运气会很不好。从GET到POST没有真正的“切换”;他们是不同的http方法。你为什么做这个? –

回答

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

看来我的web服务不支持HttpPost。谁能告诉我如何使用SOAP做同样的事情? –

相关问题