2016-05-13 36 views
1

我是使用WCF框架開發的C#webservice的新手。我必須在URL中發佈數據。我的網址類似於http://www.example.com/abc/DGLC.svc/login,我必須使用post方法傳遞數據。參數如下格式。在Android中如何將數據發佈到WCF中創建的webservice?

{

「用戶名」: 「管理」,

「密碼」: 「ABCD1234」,

「DiviceType」: 「窗口」,

「的UniqueID」:「 deviceidneedtopasshere「

}

請幫我,如何實施th是一種WS。提前致謝。

+0

證明你有試過嗎? –

+0

這是一個肥皂調用 –

+0

@VishalMokal是的這是一個SOAP調用 – Philliphe

回答

1

創建一個表示您的json字段的類。並在你的web服務中通過這個方法參數。並在backthread(的AsyncTask)

public static String postAPIResponse(String url, String data) { 

     HttpURLConnection con = null; 
     InputStream inputStream; 
     StringBuffer responses = null; 
     try { 
      URL urlObject = new URL(url); 
      con = (HttpURLConnection) (urlObject.openConnection()); 

      con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      con.setRequestMethod("POST"); 
      con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
      con.setRequestProperty("Content-Length", Integer.toString(data.getBytes().length)); 
      con.setRequestProperty("Content-Language", "en-US"); 
      if (Cookie.getCookie() != null) 
       con.addRequestProperty("Cookie", Cookie.getCookie()); 

      con.setUseCaches(false); 
      con.setDoInput(true); 
      con.setDoOutput(true); 

      //Send request 
      DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
      wr.writeBytes(data); 
      wr.flush(); 
      wr.close(); 

      //Get Response 
      if (con.getResponseCode() == 200) { 


       InputStream is = con.getInputStream(); 
       BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
       String line; 
       responses = new StringBuffer(); 
       while ((line = rd.readLine()) != null) { 
        responses.append(line); 
       } 
       rd.close(); 

      } else { 

       inputStream = new BufferedInputStream(con.getErrorStream()); 
       return convertInputStreamToString(inputStream); 
      } 


     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      assert con != null; 
      con.disconnect(); 
     } 
     return responses != null ? responses.toString() : ""; 
    } 


static public String convertInputStreamToString(InputStream inputStream) throws IOException { 
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
    String line; 
    String result = ""; 
    while ((line = bufferedReader.readLine()) != null) { 
     result += line; 
    } 
     /* Close Stream */ 
    inputStream.close(); 
    return result; 
} 

運行這個方法,其中數據是JSON字符串對象新JsonObject.accumulate()...等與您的服務對象映射

相關問題