2011-08-30 73 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做同樣的事情? –

相關問題